从标记访问UITableViewCell

时间:2014-05-19 16:54:56

标签: ios

我试图以这种方式更改UITableViewCell上的详细信息标签:

UITableViewCell *cell = (UITableViewCell *)[folderDisplayTableView viewWithTag:CODE_FOR_OPEN_FOLDER];
if (cell != nil)
{
   cell.detailTextLabel.text = @"YO";
}

如果CODE_FOR_OPEN_FOLDER对应一个标记,则该方法有效,否则应用程序崩溃。我无法理解为什么因为我检查细胞是否为零......

2 个答案:

答案 0 :(得分:1)

请注意,0是UIView的标签属性的默认值,在这种情况下,您应该在通过viewWithTag获取单元格之前检查您的标签变量,而不是检查单元格!= nil,我确定单元格!= nil当CODE_FOR_OPEN_FOLDER = 0时,在这种情况下,cell.detailTextLabel.text可能会导致崩溃,这里是你的代码编辑,

UITableViewCell *cell;
if (CODE_FOR_OPEN_FOLDER == 11) // for example 11 is a tag value
{
    cell = (UITableViewCell *)[folderDisplayTableView viewWithTag:CODE_FOR_OPEN_FOLDER];
}
cell.detailTextLabel.text = @"YO"; // there's nothing wrong sending message to nil object

希望它有所帮助。

答案 1 :(得分:0)

为什么不使用didSelectRowAtIndexPath方法,然后使用标记

获取行和文本的数量