【问题标题】:Custom UITableViewCell not appearing when row height is set设置行高时不出现自定义 UITableViewCell
【发布时间】:2010-06-08 00:08:15
【问题描述】:

我有一个在 IB 中创建的自定义 UITableViewCell。当我不超车时,我的标签会显示:

- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath

但是我的内容被压扁了。我希望高度为 120px,所以我有以下内容:

- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{   CGFloat rowHeight = 120;
    return rowHeight;
}

我不知道为什么我的 UITableViewCell 里面的内容突然消失了?

更新: 以下是我创建单元格的方法:

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

    static NSString *MyIdentifier = @"ARCell";
    ARObject *myARObject;
    myARObject = [items objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"ARCell" owner:self options:nil];
        cell = arCell;
        self.arCell = nil;
    }

    UILabel *label;
    label = (UILabel *)[cell viewWithTag:0];
    label.text = [NSString stringWithFormat:@"%@", myARObject.username];

    label = (UILabel *)[cell viewWithTag:1];
    label.text = [NSString stringWithFormat:@"%@", myARObject.created_at];

    label = (UILabel *)[cell viewWithTag:2];
    label.text = [NSString stringWithFormat:@"%@", myARObject.articleTitle];

    label = (UILabel *)[cell viewWithTag:3];
    label.text = [NSString stringWithFormat:@"%@", myARObject.intro_text];

    return cell;
}

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview


    【解决方案1】:

    在 IB 中你是否也设置了视图大小的高度?

    如果是这样,您可以尝试返回 120 而不是将其声明为 CGFloat

    编辑:另一种想法,确保您保存在 IB 中...这是显示错误的主要原因!

    【讨论】:

    • 在 IB 的 Size 选项卡下,我设置了 W: 320 和 H: 120 这是正确的位置吗?
    【解决方案2】:

    我认为您为 tableview 单元格加载笔尖的方式有些可疑。与其将 cellview 连接到文件所有者的插座,不如尝试以下方法:

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ARCell" owner:self options:nil];
            cell = (UITableViewCell *)[nib objectAtIndex:0];
    

    至少我建议你在该行上调试并设置一个断点来验证

    1. 细胞确实存在
    2. 单元格具有您期望的子视图
    3. 单元格具有正确的框架和 表格视图单元格的边界

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 2016-06-11
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      相关资源
      最近更新 更多