【问题标题】:Subviews of UITableViewCell seem to randomly disappear after dequeueReusableCellWithIdentifier在 dequeueReusableCellWithIdentifier 之后 UITableViewCell 的子视图似乎随机消失
【发布时间】:2014-02-23 11:46:21
【问题描述】:

在我的 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 重新出现。

【问题讨论】:

    标签: ios iphone uitableview ios7


    【解决方案1】:

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

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

    【讨论】:

    • 好的,使用 textLabel 和 detailTextLabel 解决了这个问题。如果我有一个自定义原型单元格并且不想继承 UITableViewCell,我如何获得我感兴趣的各个子视图?
    • 只是子类化它。它比其他替代方案更容易,也更简单,并且没有真正的理由子类化。
    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2017-04-20
    • 2011-10-08
    • 1970-01-01
    相关资源
    最近更新 更多