访问自定义单元格中的标签值

时间:2014-03-16 20:31:51

标签: iphone xcode5

我是Xcode的新手,所以请和我一起。我已经能够使用自定义单元格填充我的tableview,自定义单元格上有3个标签。

选择行时,我需要访问其中一个标签(myDateLabel)的值。

对于“标准”单元格,我知道我可以使用类似的东西:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *theValue = cell.textLabel.text;

但我在使用自定义单元格时无法弄清楚如何做到这一点,任何帮助都会受到赞赏,我敢肯定我错过了一些简单的东西

2 个答案:

答案 0 :(得分:0)

您可以在故事板中设置标签的标签属性(如果您正在使用它),也可以在创建标签时设置代码。

label1.tag = 1001;
label2.tag = 1002;
label3.tag = 1003;

然后你可以枚举单元格的子视图并检查子视图的标记

for (UIView *subview in customCell.subviews)
    switch (subview.tag)
    {
          case 1001:
           NSString *s = [(UILabel)subview text];
           break;
    }

答案 1 :(得分:0)

更改您的代码:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *theValue = cell.textLabel.text;

要:

CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.myDateLabel.text = @"text";

这里有一个很棒的初学者教程:http://www.appcoda.com/customize-table-view-cells-for-uitableview/