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 = [UI开发者_运维知识库Label 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.)
-(void)touched:(id)Sender {
// Here i want to get the UILabels for each cell. Such as nowArtist.
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)sender;
UIView *contentView = button.superview;
UIView *viewWithTag1 = [contentView viewWithTag:1];
if ([viewWithTag1 isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *)viewWithTag1;
// You now have the title label.
}
// Repeat the above for each tag.
}
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:
cell.nowTitle.text = @"In My Heart";
UIButton has a property called "tag", it's purpose is exactly that, it used to identify the button, try setting a value that will help you find the needed by setting an index in the tag property.
Im getting that you want to execute specific code at the touch event of the table row.. See if this makes sense to you.
- Add UiButton as accessory view to the cell inside the cellForRowAtIndexPath UIButton *button = [[[UIButton alloc] initWithFrame:CGRectZero] autorelease]; //You can specify image for button here. [cell addSubview:button]; cell.accessoryView = button;
- I hope you are not hard coding all the Artist and Titles.. lets say you are using Array. On the touch event of any of the cell. in the UITableView method didSelectRowAtIndexPath you will get the row number... use it to get the Label text from the Array.
There is another method which most of us are not familiar with and I think its very useful. Check below.
- (void)touched:(id)sender {
UIButton *btn = (UIButton *)sender;
NSIndexPath *indexpath = [self.tableview indexPathForRowAtPoint:btn.center];
RadioCustomCell *currentCell = (RadioCustomCell *)[tableview cellForRowAtIndexPath:indexpath]
UILabel *titleLabel = (UILabel *)[currentCell viewWithTag:YourTag];
}
精彩评论