我正在使用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;

}


如何解决这个问题?

最佳答案

似乎是可重用的电池问题。要解决此问题,您应该清楚地设置单元格的条件。例如:

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


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

关于ios - 自定义tableviewCell内容宽度与tableView不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57282938/

10-12 18:08
查看更多