繁体   English   中英

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

[英]Subviews of UITableViewCell seem to randomly disappear after dequeueReusableCellWithIdentifier

在我的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重新回到视野。

你在做什么错了。 不要依赖于私有类的视图层次结构,当然不依赖于层次结构中视图的数量,并且实际上也不依赖于位于子视图数组中某个位置的视图。 可能由于子视图“消失”而无法输入错误块-可能已经添加了一个额外的视图,您所要检查的只是子视图的数量等于2。

如果使用的是标准单元textLabel detailTextLabel ,请使用textLabeldetailTextLabel属性。 如果您使用的是子类,请使用插座。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM