【问题标题】:How to get UITableView Label Text string - Custom Cell如何获取 UITableView 标签文本字符串 - 自定义单元格
【发布时间】:2013-09-08 06:02:10
【问题描述】:

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

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

但是,当您使用自定义单元格时,如何做到这一点?因为我的 UILabel 的名字是“type_label”,它已经在我的 CustomCell XIB 的头文件中定义了。

所以在 Xcode 中我不能使用:

cell.type_label.text"

在以下函数中:

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

谢谢,丹。

【问题讨论】:

  • 试试:YourCustomCellClass *cell = (YourCustomCellClass *)[tableView cellForRowAtIndexPath:indexPath];

标签: ios objective-c string uitableview


【解决方案1】:

在 tableViewDidSelectRow 中你需要做的就是:

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

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

这应该可以满足您的需求。

【讨论】:

    【解决方案2】:

    试试下面的代码 - 你已经在 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;
    
    }
    

    【讨论】:

    • 不回答问题。
    • @CW0007007 通过实现上面的代码 cell.type_label.text 是可能的,或者在我的情况下 cell.nameLabel.text .... 在我的项目中我已经实现了很多次......跨度>
    • 没有显示他如何在 didSelectRow 中访问它。您只是向他展示了他如何设置 customCell 标签的文本,而不是他的要求。此外,您将静态 BOOL 设置为 NO,然后您正在检查它是否为 NO。永远不会……那是多余的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多