自定义 UITableViewCell 标签消失

Posted

技术标签:

【中文标题】自定义 UITableViewCell 标签消失【英文标题】:Custom UITableViewCell labels disappearing 【发布时间】:2012-07-24 23:27:39 【问题描述】:

我目前正在使用自定义 UITableViewCell 构建一个表格视图。我以前做过很多次,但由于某种原因,细胞没有正确显示。另外,当我向上滚动时,文本标签开始消失,此时我不知道为什么。这是 cellForRowAtIndexPath 方法:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"Row: %i Section: %i", indexPath.row , indexPath.section);
HollerCommentCell *cell = (HollerCommentCell *)[table dequeueReusableCellWithIdentifier:@"HollerCommentCell"];
if( !cell )

    //Initialize a new cell
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HollerCommentCell" owner:nil options:nil];
    for( id currentObject in topLevelObjects )
    
        if([currentObject isKindOfClass:[HollerCommentCell class]])
            cell = (HollerCommentCell*)currentObject;
            break;
        
    

[[cell name]setText:@"Nick"];
[[cell commentText]setText:@"Hello"];
return cell;

这里也是我的 numberOfRowsInSection 方法的副本:

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
    return ( section == 0 ) ? 5 : 0;

以下是其工作原理的屏幕截图:

不知道现在发生了什么。想法?

【问题讨论】:

所以只是为了弄清楚这一点 - 当你第一次看到这个视图时,一切看起来都很完美。但你不能真正向右滚动,你只有 5 个单元格?细胞数量有变化吗?如果确实如此,您会立即重新加载表格吗? 【参考方案1】:

您似乎关闭了剪辑功能,并且 commentText 标签的位置太低了 44 点。效果是commentText 出现在它应该设置的单元格正下方的行中。

也许你应该在你的单元格中使用 NSLog 视图并查看它们的框架的位置。

【讨论】:

是的,我也在想同样的事情——但我觉得这一切都是第一次完美展示——让我们看看答案:-) 是的,你没看错 :) 我完全没有根据评论长度调整评论文本字段的大小。看来我不擅长复制自己的代码!

以上是关于自定义 UITableViewCell 标签消失的主要内容,如果未能解决你的问题,请参考以下文章

内部带有TextView的快速自定义UITableViewCell消失

滚动时 UITableViewCell 的奇怪行为,UIButtons 消失

出队后,CALayer 在自定义 UITableViewCell NIB 上消失

自定义 UITableViewCell 不显示标签

UITableViewCell 中的自定义标签问题

自定义 UITableViewCell 标签文本在取消选择时不会变回