我正在以编程方式设置 UITableView。我希望单元格的内容跨越屏幕的整个宽度。我已经成功地将单元格设置为跨越屏幕的宽度,但内容和分隔符仍然显着插入(下面的 iPad 屏幕截图)。
这是我的视图控制器实现中的 tableview 布局设置:
- (void) viewDidLoad {
[super viewDidLoad];
// table layout
self.tableView.rowHeight = 192;
UILayoutGuide *margins = [self.view layoutMarginsGuide];
[self.tableView.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor] ;
[self.tableView.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
CGRect tableRect = self.view.frame;
self.tableView.frame = tableRect;
// table colors
self.tableView.backgroundColor = [UIColor grayColor];
self.tableView.separatorColor = [UIColor grayColor];
UIView *backView = [[UIView alloc] init];
[backView setBackgroundColor:[UIColor grayColor]];
[self.tableView setBackgroundView:backView];
}
然后我设置单元格的内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
cell.indentationWidth = 0;
cell.indentationLevel = 0;
cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0);
cell.contentView.backgroundColor = [UIColor purpleColor];
return cell;
}
单元格背景是蓝色的,它跨越了屏幕的宽度。我截图中的紫色区域是 contentView,如你所见,它没有延伸到屏幕的右边缘,并且单元格文本在左侧插入。分隔符也插入在左右两侧。