Access to custom label properties in didSelectRowAtIndexPath file

I have a UILabel for each cell in cellForRowAtIndexPath.

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

I want to access this line "myString" in the didSelectRowAtIndexPath file using indexpath.

 NSString *anotherString = cell.textLabel.text; 

returns null.

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

My question is: how can I access the text in the UILabel that I apply to the cell in the didSelectRowAtIndexPath file?

In addition, writing a cell in the didSelectRowAtIndexPath file returns cell: <UITableViewCell: 0x5dcb9d0; frame = (0 44; 320 44); autoresize = W; layer = <CALayer: 0x5dbe670>> 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; 
+6
objective-c iphone
source share
2 answers

In this line of code:

 NSString *anotherString = cell.textLabel.text; 

How do you get the cell? Is it zero? Also, the textLabel field that you are referring to is the default label in the UITableViewCell, not the label you add to -cellForRowAtIndexPath. Here's how you can get a cell from -didSelectRowAtIndexPath:

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

The problem at this point is that you cannot access UILabel by name, however you can access it if you set the tag. So after creating your UILabel, set the tag as follows:

 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 tag 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]); } 
+18
source share

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

Find a UITableViewCell for more details.

+1
source share

All Articles