Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-07-2010, 01:02 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 4
Default UIButtons in UITableViewCells

Hello to all,

I'm new to this forum and to SDK programming and I'm trying to achieve this as a test application. I have a window-based application with two views and a navigation bar. The first view has a button which pushes the view on a tableview, The tableview loads the data from an array whose elements fill one label for each cell. The cells also have another label and a button. The "big" label loads the data correctly, as well as the second ("small") label shows correctly the given text. The problems begin with the buttons. The buttons should change the text of the "big" label into something else I declared within a method in the cell definition class but instead, although it compiles and runs, the applciation doesn't fill the buttons with the given text and crashes when I click any ofthe buttons. I followed the code of another user to do this but doesn't seem to work with me. I'll post here all the files I've done for this:

Code:
//
//  CustomCell.h


#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell {
	UILabel *bigLabel;
	UILabel *smallLabel;
	//UIImageView *image;-
	UIButton *button;
}
@property (nonatomic,retain) UILabel *bigLabel;
@property (nonatomic,retain) UILabel *smallLabel;
//@property (nonatomic,retain) UIImageView *image
@property (nonatomic,retain) IBOutlet UIButton *button;
//-(IBAction)UpdateLabel:(UIButton *)sender;
@end

Code:
//
//  CustomCell.m


#import "CustomCell.h"


@implementation CustomCell

@synthesize bigLabel;
@synthesize smallLabel;
//@synthesize image;
@synthesize button;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // Initialization code
		bigLabel = [[UILabel alloc]init];
		
		bigLabel.textAlignment = UITextAlignmentLeft;
		bigLabel.font = [UIFont systemFontOfSize:13];
		
		smallLabel = [[UILabel alloc]init];
		
		smallLabel.textAlignment = UITextAlignmentLeft;
		smallLabel.font = [UIFont systemFontOfSize:7];
		
		button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] init];
		
		[self.contentView addSubview:bigLabel];
		[self.contentView addSubview:smallLabel];
		[self.contentView addSubview:button];
	}
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];
	[button addTarget:self action:@selector(UpdateLabel:) forControlEvents:UIControlEventTouchUpInside];

    // Configure the view for the selected state
}


- (void)dealloc {
    [super dealloc];
}

- (void)layoutSubviews{
	[super layoutSubviews];
	
	CGRect contentRect = self.contentView.bounds;
	
	CGFloat boundsX = contentRect.origin.x;
	
	CGRect frame;
	
	frame = CGRectMake(boundsX + 0, 5, 200, 25);
	
	bigLabel.frame = frame;
	
	frame = CGRectMake(boundsX + 0, 30, 100, 12);
	
	smallLabel.frame = frame;

	frame = CGRectMake(boundsX + 270, 5, 40, 25);
	
	button.frame = frame;	
	
}

- (void)UpdateLabel:(UIButton *)sender{
	bigLabel.text = @"Asd!";
}

@end
Code:
//
//  ListaRicette.h

#import <UIKit/UIKit.h>


@interface ListaRicette : UITableViewController {
	NSMutableArray *lista;
}
@property (nonatomic,retain) NSMutableArray *lista;
@end

Code:
//
//  ListaRicette.m

#import "CustomCell.h"
#import "ListaRicette.h"


@implementation ListaRicette

@synthesize lista;

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


- (void)viewDidLoad {
    [super viewDidLoad];

	self.title = @"Le ricette sfiziose"; 
	lista = [[NSMutableArray alloc] initWithObjects:@"Polenta",@"Salciccie",@"Piselli",@"Cotechino",@"Ravioli",@"Pancetta",nil];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return[lista count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSString *CellIdentifier = [NSString stringWithFormat:@"%@%d",@"CellID_", indexPath.row];
	
	CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if(cell == nil){
		cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
	}
	cell.bigLabel.text =[lista objectAtIndex:indexPath.row];   
	//[cell setTag:[indexPath row]];  
	cell.smallLabel.text = @"Testo piccolo x tutti";
	[cell.button setTitle:@"LOL" forState:UIControlStateNormal];
	[cell.button addTarget:self action:@selector(UpdateLabel:) forControlEvents:UIControlEventTouchUpInside];
	return cell;
	/*UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
	if (cell == nil){
		cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:@"cellID"] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
	}
	cell.textLabel.text =[lista objectAtIndex:indexPath.row];
	cell.detailTextLabel.text =[lista objectAtIndex:indexPath.row];//@"LOL";
    return cell;*/
	
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end
I tried to search the forum for a solution but nothing seemed to help. This is really driving me crazy so any kind of help will be appreciated. Sorry for my awful english and thanks in advance.
iPwned is offline   Reply With Quote
Old 01-08-2010, 03:18 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 4
Default

Up. Please somebody help me...
iPwned is offline   Reply With Quote
Old 01-08-2010, 08:35 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 4
Default

Up. I'm desperate...
iPwned is offline   Reply With Quote
Old 01-08-2010, 11:35 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])

....


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSString *CellIdentifier = [NSString stringWithFormat:@"%@%d",@"CellID_", indexPath.row];
	
	CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if(cell == nil){
		cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
	}
	cell.bigLabel.text =[lista objectAtIndex:indexPath.row];   
	//[cell setTag:[indexPath row]];  
	cell.smallLabel.text = @"Testo piccolo x tutti";
	[cell.button setTitle:@"LOL" forState:UIControlStateNormal];
	[cell.button addTarget:self action:@selector(UpdateLabel:) forControlEvents:UIControlEventTouchUpInside];
	return cell;
	/*UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
	if (cell == nil){
		cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:@"cellID"] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
	}
	cell.textLabel.text =[lista objectAtIndex:indexPath.row];
	cell.detailTextLabel.text =[lista objectAtIndex:indexPath.row];//@"LOL";
    return cell;*/
	
}
Those should match.

See the Apple sample called TaggedLocations for an example of buttons in table view cells.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 01-11-2010, 03:13 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 4
Default

Well, thanks! I'll watch the piece of code and tell you what I managed to do.
iPwned is offline   Reply With Quote
Reply

Bookmarks

Tags
action, uibutton, uitableview, uitableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 271
20 members and 251 guests
@sandris, AdamL, ADY, Dani77, diyora, FAED, F_Bryant, GHuebner, HDshot, headkaze, mer10, Oral B, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,750
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:57 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0