UITableViewCellStyleSubtitle 单元格的分隔线没有占据整个宽度

5

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

当使用名为SubtitleUITableViewCellStyleSubtitle单元格类型时,横向线条无法到达屏幕左侧:

应用截图

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

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

我还尝试将自定义插入设置为自定义,并将左侧设置为0像素(对于Table ViewMyCell都是如此),但左侧仍有间隙:

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];
    }
} 
*/

1
在iOS 8中,您需要添加一些代码来实现此操作。请检查答案。 - Ashish Kakkad
1
亲爱的Alexander,好久不见! :) 我能说服你不要这样做吗?有美学原因解释为什么需要插图以及为什么在插入图片时需要更改插图。 - Léo Natan
1个回答

10

将表格视图分割线插入设置为零,不仅适用于表格视图单元格。

enter image description here

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;
}

预览图片:

输入图像描述


1
好的,那就等待另一个答案@AlexanderFarber。 - Ashish Kakkad
2
太棒了!可惜没有诺贝尔奖来表彰绕过愚蠢的iOS行为。当然,这种解决方案是有代价的:开发者可能有其他原因不想改变单元格边距。但是还有其他方法可以解决这个问题(添加一个具有自己边距的子视图)。 - matt
2
只是为了明确:没有必要涉及表格。在 cellForRowAtIndexPath: 中设置三个内容:(1) 单元格的分隔符插入; (2) 单元格的布局边距; (3) 单元格的 preservesSuperviewLayoutMargins。这样就可以了。 - matt
@AshishKakkad 感谢您的回答。然而,这样做后仍然留下了一些空格。有没有办法也覆盖它们? - A_G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接