UITableViewCellStyleSubtitle单元格的分隔线没有占满全部宽度

我在GitHub上为我的问题准备了一个简单的testing项目 。

使用UITableViewCellStyleSubtitletypes的单元格(在Xcode Interface Builder中称为“ 字幕” )时,由于某些原因,水平线不能到达屏幕的左侧:

应用截图

在上面的截图中,您可以看到“空单元格”中的分隔线占据了整个宽度。

首先,我认为图标(大小: 46 x 46像素 )不适合单元格,并试图增加行高,但这并没有帮助。

此外,我试图将自定义插入设置为自定义,并有为0像素(对于表视图MyCell ),但左侧仍有一个空白:

Xcode截图

这里是我的TableViewController.m的源代码:

- (void)viewDidLoad { [super viewDidLoad]; self.items = [[NSMutableArray alloc] initWithArray:@[ @{@"title": @"Title 1", @"subtitle": @"Sub Title 1", @"icon": @"signal4.png"}, @{@"title": @"Title 2", @"subtitle": @"Sub Title 2", @"icon": @"signal1.png"}, @{@"title": @"Title 3", @"subtitle": @"Sub Title 3", @"icon": @"signal2.png"}, @{@"title": @"Title 4", @"subtitle": @"Sub Title 4", @"icon": @"signal3.png"} ]]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.items.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath]; NSDictionary* item = [self.items objectAtIndex:indexPath.row]; cell.textLabel.text = item[@"title"]; cell.detailTextLabel.text = item[@"subtitle"]; UIImage* icon = [UIImage imageNamed:item[@"icon"]]; [cell.imageView setImage:icon]; return cell; } 

更新:从这个答案的源代码已经帮助我在最后: iOS 8的UITableView分隔符插图0不工作

固定截图

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // Remove seperator inset if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } // Prevent the cell from inheriting the Table View's margin settings if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { [cell setPreservesSuperviewLayoutMargins:NO]; } // Explictly set your cell's layout margins if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } } /* -(void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; // Force your tableview margins (this may be a bad idea) if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { [self.tableView setLayoutMargins:UIEdgeInsetsZero]; } } */ 

将表视图分隔符插入添加到零。 不只是表格视图单元格。

在这里输入图像说明

cellForRowAtIndexPath添加下面的代码:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //.... if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0) { cell.layoutMargins = UIEdgeInsetsZero; cell.preservesSuperviewLayoutMargins = NO; } } 

与您的代码整合:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath]; if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0) { cell.layoutMargins = UIEdgeInsetsZero; cell.preservesSuperviewLayoutMargins = NO; } NSDictionary* item = [self.items objectAtIndex:indexPath.row]; cell.textLabel.text = item[@"title"]; cell.detailTextLabel.text = item[@"subtitle"]; UIImage* icon = [UIImage imageNamed:item[@"icon"]]; [cell.imageView setImage:icon]; return cell; } 

查看预览:

在这里输入图像说明