【问题标题】:UITableViewCell not appearing/reusing correctly from NIBUITableViewCell 未从 NIB 正确显示/重用
【发布时间】:2010-06-08 16:34:46
【问题描述】:

我从 NIB 创建了一个自定义 UITableViewCell。当没有设置 rowHeight 时,一切都会出现(尽管一切都被压扁了)。我不确定我是否正确地重用和创建单元格:

// Customize the appearance of table view cells.
- (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;
}

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

问题:

  1. 设置 rowHeight 时,并非所有标签都会出现。如果没有设置 rowHeight,标签会被压扁
  2. 未设置 rowHeight 时,选择一个项目会显示所有标签

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview interface-builder


    【解决方案1】:

    删除 else 子句

    【讨论】:

      【解决方案2】:

      您调用 loadNibNamed,但不要将结果存储在任何地方!完全不明白为什么你的代码可以工作......无论如何,盲写我会尝试这样的事情:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *MyIdentifier = @"ARCell";
          ARObject *myARObject = [items objectAtIndex:indexPath.row];
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
          if (cell == nil) {
              NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ARCell" owner:self options:nil];
              cell = (UITableViewCell *)[nib objectAtIndex:0];
         }
      
          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;
      }
      
      - (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          return 105.0;
      }
      

      【讨论】:

      • 我得到了同样的结果。而不是 cell = (UITableViewCell *)[nib objectAtIndex:0];我将它分配给我的 h 文件中的 UITableViewCell 属性 arCell
      • 我正在关注 Apple TableView 编程指南中的示例:developer.apple.com/iphone/library/documentation/userexperience/…
      • 已检查,示例看起来像您的代码。然后只需检查您的 arCell IBOutlet “由于您建立的插座连接,tvCell 插座现在具有对从 nib 文件加载的单元格的引用。”
      • 只是为了检查:单元重用或大小的问题?您可以使用 NSLog(@"") 调用验证重用,但调整重用单元的大小是完全不同的。您的代码使用硬编码值 105,我认为这是所有单元格的正确值?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      相关资源
      最近更新 更多