如何获取UITableView标签文本字符串 – 自定义单元格

我有一个带CustomCell的UITableView。 自定义单元格包含UILabel和UIImageView。

我在网上看到,当用户按下单元格时,可以从普通的UITableView单元格中获取文本并将其存储在字符串中。

但是,当您使用自定义单元格时,如何执行此操作? 因为我的UILabel的名称是“type_label”,它已经在我的CustomCell XIB的头文件中定义。

所以在Xcode中我无法使用:

cell.type_label.text" 

在以下function中:

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

谢谢,丹。

在tableViewDidSelectRow中,您需要做的就是:

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

这应该让你想要你需要。

尝试以下代码 – 您已在CCustomCellClass中创建type_label标签的IBOutlet
(file – > new – > subclass – > UITableViewCell – >将其命名为CustomCellClass)
然后实现下面的代码

  -(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; }