【问题标题】:Loading cell in table view from xib bug从 xib 错误加载表格视图中的单元格
【发布时间】:2016-09-22 13:20:28
【问题描述】:

我正在从 xib 加载单元格。但随后表格视图控制器打开单元格是白色的。只有在单击单元格后才会显示实验室和其他内容。

 - (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.tableView registerNib:[UINib nibWithNibName:@"FixedCell" bundle:nil] forCellReuseIdentifier:@"FixedCell"];
    }

    //cellForRow......
    if(indexPath.section==1){
        if(indexPath.row==0){
        FixedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FixedCell"];
        return cell;
       }
   }

谁能找到,这里有什么问题?

【问题讨论】:

  • 只需编辑您的问题并提供更多详细信息,以便我们更好地理解并相应地回答..

标签: ios objective-c uitableview xib


【解决方案1】:

使用此代码可能会有所帮助...

-(void)tableView:(UITableView *) willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    FixedCell *newCell = (FixedCell *)cell;
    [newCell layoutIfNeeded];


}

【讨论】:

    【解决方案2】:

    不要在UITableView中加载UINib,只需在viewDidLoad方法中加载它..

    使用这种方法

    UINib *nib = [UINib nibWithNibName:@"FixedCell" bundle:nil];
    [self.tablView registerNib:nib forCellReuseIdentifier:@"FixedCell"];
    

    【讨论】:

      【解决方案3】:

      目标 C

      试试这个...

      cell =(FixedCell *)[tableView dequeueReusableCellWithIdentifier:@"FixedCell" forIndexPath:indexPath];
      
      
      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
          [cell layoutIfNeeded];
          CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
          return size.height;
      }
      

      斯威夫特:

      var cell: FixedCell! = tableView.dequeueReusableCellWithIdentifier("FixedCell", forIndexPath: indexPath) as! HistoryTableViewCell
      
      if (cell == nil) {
          cell = FixedCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "FixedCell")
      }
      

      【讨论】:

      • 我已经为这个单元格准备了:- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.section == 1 && indexPath.row == 0) { 返回 70; } 返回 35; } 和 xib 一样的高度
      猜你喜欢
      • 1970-01-01
      • 2021-06-04
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多