UITableViewCellStyleSubtitle单元格的分隔线未占用整个宽度

Ale*_*ber 5 xcode uitableview tableview ios

我在GitHub为我的问题准备了一个简单的测试项目.

使用UITableViewCellStyleSubtitle类型的单元格(在Xcode Interface Builder中称为Subtitle)时 - 由于某种原因水平线未到达屏幕的左侧:

应用截图

在上面的屏幕截图中,您可以看到"空单元格"中的分隔线占据了整个宽度.

首先,我认为图标(大小: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;
}
Run Code Online (Sandbox Code Playgroud)

更新:这个答案的源代码最终帮助了我: 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];
    }
} 
*/
Run Code Online (Sandbox Code Playgroud)

Ash*_*kad 11

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

在此输入图像描述

将此代码添加到cellForRowAtIndexPath:

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

与您的代码集成:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

查看预览:

在此输入图像描述

  • 辉煌!遗憾的是,没有诺贝尔奖可以解决愚蠢的iOS行为.当然,这个解决方案需要付出代价:开发人员可能有其他原因想要更改单元格边距.但是有很多方法(添加一个带有自己边距的子视图). (2认同)
  • 需要明确的是:没有必要参与该表.在`cellForRowAtIndexPath:`中设置三件事:(1)单元格的分隔符插入; (2)单元格的布局边距; (3)单元格的'preservesSuperviewLayoutMargins`.那样做. (2认同)