views:

244

answers:

3

I have a UILabel for each cell at cellForRowAtIndexPath.

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;

I want to access that string "myString" at didSelectRowAtIndexPath using indexpath.

NSString *anotherString = cell.textLabel.text;

returns null.

Now if at cellForRowAtIndexPath, I did something like cell.textLabel.text = theString; then the didSelectRowAtIndexPath returns the appropriate cell.

My question is, how can I access the text in the UILabel I apply to the cell, at didSelectRowAtIndexPath?

Also, logging the cell in didSelectRowAtIndexPath returns cell: <UITableViewCell: 0x5dcb9d0; frame = (0 44; 320 44); autoresize = W; layer = <CALayer: 0x5dbe670>>

Edit:

    NSString *myString = [[results objectAtIndex:indexPath.row] valueForKey:@"name"];
//cell.textLabel.text = myString;

CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
CGFloat height = 20;
CGRect frame = CGRectMake(10.0f, 10.0f, width, height);

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor whiteColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont systemFontOfSize:14.0f];
[cell.contentView addSubview:cellLabel];
[cellLabel release];

return cell;
+1  A: 

Can you post the code that you assign the cellLabel to the cell? Did you do something like this: cell.textLabel = cellLabel?

Look for UITableViewCell for more details

vodkhang
Code requested has been posted under "Edit:"
Oh Danny Boy
My problem is if I directly set the cell.textLabel.text, then I get that and my custom UILabel overlapping. Which is a complete pain.
Oh Danny Boy
But the cell already has a textLabel? Do you really want to add another UILabel as subview? My idea is that you can set the textLabel directly using cell.textLabel = cellLabel not doing [cell.contentView addSubView:cellLabel]
vodkhang
The problem with using cell.textLabel is it cuts off the string after 8 characters. I have no clue why. Was unable to troubleshoot so I am trying to add a label at the proper length so I can show more than 8 character strings on one line without "...'s" trailing half the word.
Oh Danny Boy
Thank you for your help.
Oh Danny Boy
+2  A: 

In this line of code:

NSString *anotherString = cell.textLabel.text;

How are you obtaining the cell? Is it nil? Also, the textLabel field you're accessing is the default label in the a UITableViewCell and not the label you are adding in -cellForRowAtIndexPath. Here is how you can get the cell from -didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tv 
                 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tv cellForRowAtIndexPath:indexPath];

}

The issue at this point, though, is that you can't access the UILabel by name, however, you can access it if you've set a tag. So, when you create your UILabel, set the tag like this:

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor whiteColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont systemFontOfSize:14.0f];

// Set the tag to any integer you want
cellLabel.tag = 100;

[cell.contentView addSubview:cellLabel];
[cellLabel release];

So, now you can access the UILabel by tag:

- (void)tableView:(UITableView *)tv 
          didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tv cellForRowAtIndexPath:indexPath];

    UILabel *label = (UILabel*)[cell viewWithTag:100];

    NSLog(@"Label Text: %@", [label text]);
}
Matt Long
Very interesting approach. However, with this implementation I get warning: incompatible Objective-C types initializing 'struct UIView *', expected 'struct UILabel *' at line " UILabel *label = [cell viewWithTag:100];"
Oh Danny Boy
Just type cast it to a UILabel*, e.g. UILabel *label = (UILabel*)[cell viewWithTag:100]; Check the update to my answer.
Matt Long
I would of never thought of this. It worked perfectly. Is this the standard way to grab cell text when using a custom label at cellForRowAtIndexPath? There is a lot of information on the net for creating custom cells, not so much in accessing them. Thank you for the help.
Oh Danny Boy
I often create a custom cell and use it instead of a standard UITableViewCell depending on what I need to display. If all you need is a label and/or a detail label, I suggest you use the standard UITableViewCell. (The detail label only shows if you create your UITableViewCell using the UITableViewCellStyleSubtitle constant in the cell init.) Otherwise, roll your own: http://www.e-string.com/content/custom-uitableviewcells-interface-builder
Matt Long
This helped me big time. Thanks!
meridimus
A: 

Can I mod a label in didSelectRowAtIndexPath with this method? for example her size? thanks

paul_1991