Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didSelectRowAtIndexPath for Custom label Cell

I am using this method to add an UILabel to my UITableView and its working fine, but I also need to create a method for selecting the row and save into an temp String.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                             reuseIdentifier:CellIdentifier];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
    [cell.contentView addSubview:label];
    label.text = [tableArr objectAtIndex:indexPath.row];
    label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0];
    return cell;
}

but its not working. It was working fine with when I was using cell.textLabel but not with the custom label.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *lbltemp = cell1.textLabel;

    _parent.labelone.text = lbltemp.text;       
}
like image 907
Anil Avatar asked Jun 03 '13 17:06

Anil


3 Answers

You could create your own UITableViewCell subclass to allow you to get the reference to your label (and prevent you from creating multiple labels on the cell). Or you could use tags like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell = nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                         reuseIdentifier:CellIdentifier];

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
        label.tag = 123123;
        [cell.contentView addSubview:label];
    }

    UILabel *label = (UILabel *)[cell viewWithTag:123123];
    label.text = [tableArr objectAtIndex:indexPath.row];
    label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:123123];

    _parent.labelone.text = label.text;       
}

Your original code was adding a new label but then trying to get the text from the default label... It was also always creating a new cell and adding a new label to it which is quite wasteful.

Also, you shouldn't usually store text in the label, you should really to to your tableArr to get the text. The tag approach is more suited to updating the label when you reuse the cell or if you're letting the user edit the text (in a UITextField).

like image 170
Wain Avatar answered Nov 08 '22 06:11

Wain


The reason it's not working is that you're not referencing the custom label in didSelectRowAtIndexPath. The easiest way to get a reference to the custom label is using tags:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    
    static NSString *CellIdentifier = @"Cell";

    // Also, this is the proper way to use reuseIdentifier (your code is incorrect)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                             reuseIdentifier:CellIdentifier];
    }

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
    [cell.contentView addSubview:label];

    label.text = [tableArr objectAtIndex:indexPath.row];
    label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0];
    label.tag = 1;

    return cell;
}

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
    UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *customLabel = [cell viewWithTag:1];
    _parent.labelone.text = customLabel.text;       
}
like image 3
Gavin Miller Avatar answered Nov 08 '22 07:11

Gavin Miller


The problem is cell1.textLabel is not the label you created. It's simply the default Label created by the cell.

The solution could be:

Add a tag to the your label when you create your custom cell.

Say label.tag=100;

In didSelectRow,

Use

UILabel *lbltemp=(UILabel *)[cell1.contentView viewWithTag:100];

then _parent.labelone.text=lbltemp.text; should grab your text from that label.

like image 3
Ryan Huang Avatar answered Nov 08 '22 05:11

Ryan Huang