【问题标题】:Custom tableviewCell content width is different from tableView自定义 tableviewCell 内容宽度与 tableView 不同
【发布时间】:2019-12-08 12:16:32
【问题描述】:

我正在使用 iPhone7 模拟器。在我的屏幕中包含带有多个不同自定义单元格的 tableview。

当我最初加载这个屏幕时,我的 tableView 宽度是 375,cell.contentview 宽度是 320。

但是当我滚动这个屏幕时,tableview 宽度是 375,cell.contentview 宽度是 375。

//Custom Cell class

@interface CustomTableViewCell ()

@property (weak, nonatomic) IBOutlet UILabel *lblTitle;

@end

@implementation CustomTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)loadData:(NSString *)data
{
    self.lblTitle.text = data;
}

@end



//Viewcontroller class

    - (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.navigationItem.title = "profile"
    [self.tableView reloadData];
}


    //tableview delegate method called from my datasource class like 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
        return [self.datasource cellForSection:indexPath.section row:indexPath.row];
    }



 //Datasource class

- (UITableViewCell *)cellForSection:(NSInteger)section row:(NSInteger)row {

       CustomTableViewCell
 *cell = [self.view cellForReuseIdentifier:[CustomTableViewCell
 reuseIdentifierString]];

    [cell loadData:self.userRepository.currentUser.id];

    return cell;

}

如何解决这个问题?

【问题讨论】:

  • 显示你的 UI 或相关代码
  • 请显示您使用 tableView 委托的代码。也许您正在做一些 UI 更改或重绘或其他任何事情?

标签: ios swift iphone xcode uitableview


【解决方案1】:

这似乎是可重复使用的细胞问题。要解决此问题,您应该清楚地设置部分中的单元格条件。例如:

if section == 0 {
  return cellA;
} else if section == 1{
  return cellB;
} else {
  return cellB;
}

如果我的答案不是您想要的,请添加更多附加信息或代码或您的问题的屏幕截图。

【讨论】:

    【解决方案2】:

    我猜您正在对 tableView 使用约束,并且 TableView 的实际大小是在加载数据之后发生的。 如何检查: 在[self.tableView reloadData];之前打印tableview的大小

    如何解决: tableView 调整大小后重新加载数据

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多