views:

48

answers:

3

Hey!

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-funtion. I know that you must use the Sender as a pointer in some sort of way, but i don't know how.

Please help.

A: 

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.

Guy Ephraim
A: 

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";
Robot K
Okay. I did that, i added nowTitle.tag = 1;But it's not getting trough to the second if-statement.
Jensen2k
Okay, i figured out what the problem was. The button and the label are in two different views. Both subviews of RadioCustomCell.The button view is called backView, and the label view is called contentView.But, how do i access the content view. I got out to their parent view by getting the superview of the superview of the button.Please help!
Jensen2k
contentView has special meaning in UITableViewCell, so you probably don't want to be adding additional views called "contentView". This may be part of your problem. If you know your view hierarchy (and you should, since you're adding all the views), you should be able to traverse the parents and children appropriately. You may need to assign tags to more views than just the labels. (You can assign a tag in IB as well as in code.) You may also want to consider flattening your view hierarchy as well. Complex table cells can kill scrolling perf.
Robot K
Thanks for the help guys, it did solve my question, sort of. Only thing is that a new one appeared. Link here: http://stackoverflow.com/questions/3805927/problem-with-uibutton-on-subview-of-a-cell
Jensen2k
A: 

Im getting that you want to execute specific code at the touch event of the table row.. See if this makes sense to you.

  1. 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;
  2. 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.
KiranThorat