在dequeueReusableCellWithIdentifier之后,UITableViewCell的子视图似乎随机消失

时间:2014-02-23 11:46:21

标签: ios iphone uitableview ios7

在我的iOS 7.0应用程序中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...

// AttemptCell is a prototype cell, currently using the "Right Detail" preset 
// style and the little information accessory.
static NSString *CellIdentifier = @"AttemptCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

assert(cell != Nil);

if (cell.contentView.subviews.count == 2)
{
    UILabel *attemptLabel = (UILabel*)cell.contentView.subviews[0];
    attemptLabel.text = attempt.attempt;

    UILabel *analysisLabel = (UILabel*)cell.contentView.subviews[1];
    analysisLabel.text = [attempt analysis];

    cell.tag = indexPath.row;
}
else
{
    // Something has gone very wrong.
    UILabel *attemptLabel = (UILabel*)cell.contentView.subviews[0];
    attemptLabel.text = @"Error";
}

问题是为什么(UILabel *)cell.contentView.subviews [1]有时会消失,导致输入错误块。

此表视图显示了一个始终显示在最后的自定义键盘输入单元格(UITextField)。键盘输入单元也是原型,但具有不同的出队单元标识符。当键盘弹出并关闭时,会随机看到该问题。键盘弹出导致一些AttemptCells退出视图并关闭键盘导致AttemptCells重新进入视图。

1 个答案:

答案 0 :(得分:0)

你在做什么是错的。不依赖于私有类的视图层次结构,当然不依赖于层次结构中的视图数量而真的不依赖于视图位于子层的特定位置视图数组。您的错误块可能无法输入,因为子视图已“消失” - 可能已添加了额外的视图,您要检查的是子视图的计数等于2.

如果您使用的是标准单元格布局之一,请使用textLabeldetailTextLabel属性。如果您正在使用子类,请使用outlet。