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];
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]); }
Matt long
source share