I have stumbled upon a problem when adding a button to my table view cell.
Let me explain after i have added the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
RadioCustomCell * cell = (RadioCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[RadioCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell setChannelImage:[UIImage imageNamed:@"p4.png"]];
UILabel *nowTitle = [UILabel alloc];
nowTitle.text = @"In My Heart";
[cell setNowTitle:nowTitle];
UILabel *nowArtist = [UILabel alloc];
nowArtist.text = @"Moby";
[cell setNowArtist:nowArtist];
UILabel *nextTitle = [UILabel alloc];
nextTitle.text = @"Only Girl (In The World)";
[cell setNextTitle:nextTitle];
UILabel *nextArtist = [UILabel alloc];
nextArtist.text = @"Rihanna";
[cell setNextArtist:nextArtist];
// My button right here
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(6 ,31, 110, 20);
[button setImage:[UIImage imageNamed:@"radionorge.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];
[cell.backView addSubview:button];
}
return cell;
}
And the “touched”-function for the action for the button.
-(void)touched:(id)Sender {
// Here i want to get the UILabels for each cell. Such as nowArtist.
}
You see, i want to get the UILabel-texts for each cell at the touched-function.
I know that you must use the Sender as a pointer in some sort of way, but i don’t know how.
First, you’re going to want to set the tag property for each label. Using a #define or const is a good idea, but for simplicity, I’m going to assume you’ll use 1, 2, 3, and 4 for each UILabel in the order they appear in the code.
This is what you’re action code should look like. (You may need to tweak it a bit.)
One observation: You’re leaking a LOT of objects in you’re code. You don’t need to alloc a bunch of new labels each time you update a cell. Just use something like: