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

时间:2013-09-04 16:06:49

标签: ios objective-c string uitableview

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

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

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

所以在Xcode中我无法使用:

cell.type_label.text"

在以下功能中:

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

谢谢,Dan。

2 个答案:

答案 0 :(得分:12)

在tableViewDidSelectRow中你需要做的就是:

UITableViewCellCustomClass *customCell = (UITableViewVellCustomClass*)[tableView cellForRowAtIndexPath:indexPath];

NSString *labelText = [[customCell type_label] text];

这应该让你想要你。

答案 1 :(得分:-1)

尝试以下代码 - 您已在CCustomCellClass中创建type_label标签' IBOutlet
(文件 - >新 - >子类 - > 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;

}