如何消除UITableView左侧的边距,而不会在右侧产生间隙?

我的目标是iOS 7和8,我想消除在这个UITableView下面的图像左侧出现的边缘。 如果解决scheme简单/优雅,我甚至愿意接受只适用于iOS 8的解决scheme。 (我只是住在iOS 7的边缘,因为它消失了)。

这是我想它看起来像:

我已经通过堆栈溢出( 如这一个 )的许多类似的问题,或具体到分组的UITableViews( 这里和这里 ),但似乎无法得到任何工作的权利。

例如,如果我尝试使用contentInset(SO上的常见答案)来解决问题:

- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, -16, self.tableView.contentInset.bottom, self.tableView.contentInset.right); self.tableView.separatorInset = UIEdgeInsetsMake(0, 33, 0, 0); } 

然后,我最终在桌面右侧的差距:

我明白为什么会发生这种情况,整个tableview只是转移过来,如Debug View Hierarchy(debugging视图层次结构)屏幕所示:

不过,我已经尝试调整tableView框架大小来弥补,但它似乎永远不会正常工作。 再加上这看起来很乱。 一定会有更好的办法。

为了logging,这里是我的cellForRowAtIndexPath:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // get a cell that we can use] UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath]; cell.textLabel.numberOfLines = 0; cell.selectedBackgroundView = selectionColor; cell.accessoryView = [[UIImageView alloc] initWithImage:[PTStyleKit imageOfArrow]]; // Create the attributed string (text + attributes) NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:[videos objectAtIndex:indexPath.row] attributes:attributes]; // Set it in our UILabel and we are done! [cell.textLabel setAttributedText:attributedText]; NSString* filename = [filenames objectAtIndex:indexPath.row]; if (filename == nil) { filename = cell.textLabel.text; } UIImage *imageOne = [UIImage imageNamed:[NSString stringWithFormat:@"medium-%@", filename]]; cell.imageView.image = [UIImage imageWithCGImage:imageOne.CGImage scale:imageOne.size.height/44 orientation:imageOne.imageOrientation]; return cell; } 

为您的表视图设置一个清晰的背景,并设置contentInset属性。

码:

 tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0); tableView.backgroundColor = [UIColor clearColor]; 

但通过设置框架将表格视图的宽度增加15个像素。

看来唯一的解决scheme,我可以find的是子类UITableViewCell,然后重写imageView和textLabel的布局。 在研究这种方法时,我发现了这个相关的答案 。 (我想我没有意识到这一整个时间我正在寻找最初在iOS 6中的tableview行为。)

我的子类有这个唯一的重写方法:

 - (void)layoutSubviews { [super layoutSubviews]; // Slide image to the left, eliminating gutter self.imageView.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height); // Slide text label to the left, and extend width by additional // space gained by eliminating the left-most gutter self.textLabel.frame = CGRectMake((self.imageView.frame.origin.x + self.imageView.frame.size.width + 15), 0, self.textLabel.frame.size.width + 15, self.textLabel.frame.size.height); } 

我的结果tableview现在看起来完全是我想要的方式:

enter image description here

我想我只是想出了一个简单的解决scheme,我发现在stackoverflow,一种方法来调整单元格边距,在你的代码中添加此方法

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) { [tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) { [tableView setLayoutMargins:UIEdgeInsetsZero]; } if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) { cell.preservesSuperviewLayoutMargins = NO; [cell setLayoutMargins:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setSeparatorInset:)]){ [cell setSeparatorInset:UIEdgeInsetsZero]; } } 

这解决了我的问题。