How to get UITableView Label Text string - Custom cell

I have a UITableView with CustomCell. Custom cells contain UILabel and UIImageView.

I saw online that you can get text from a regular UITableView cell and save it in a row when the user clicks on the cell.

But how can you do this when using a custom cell? Exhaust my UILabel name is "type_label" and it was defined in the header file of my CustomCell XI file.

So in Xcode, I cannot use:

cell.type_label.text" 

In the following function:

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

Thanks Dan.

+4
source share
2 answers

In tableViewDidSelectRow all you have to do is:

 UITableViewCellCustomClass *customCell = (UITableViewVellCustomClass*)[tableView cellForRowAtIndexPath:indexPath]; NSString *labelText = [[customCell type_label] text]; 

This should make you want what you need.

+12
source

Try it under the code - you created a label of type_label IBOutlet in CCustomCellClass
(file -> new -> subclass -> UITableViewCell -> name it as CustomCellClass)
Then execute below code

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"Cell"; static BOOL nibsRegistered = NO; if (!nibsRegistered) { UINib *nib = [UINib nibWithNibName:@"CustomCellClass" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier]; } CustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[CustomCellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.type_label.text = @"Rocky"; // setting custom cell label return cell; } 
-one
source

Source: https://habr.com/ru/post/1500575/


All Articles