【问题标题】:Separator lines for UITableViewCellStyleSubtitle cells not taking the full widthUITableViewCellStyleSubtitle 单元格的分隔线不占全宽度
【发布时间】:2015-07-21 23:41:05
【问题描述】:

我已经为我在 GitHub 上的问题准备了 a simple test project

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

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

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

我还尝试将 Custom Insets 设置为 Custom 并将 Left 设置为 0 像素(均用于 Table View em> 和 MyCell),但左侧仍有空隙:

这里是我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 separator inset 0 not working

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

【问题讨论】:

  • 在 iOS 8 中,您必须添加一些代码才能这样做。检查答案
  • 亚历山大,好久不见! :) 我可以说服你不要这样做吗?有插图是有美学原因的,为什么有图像时插图会改变。

标签: ios xcode uitableview tableview


【解决方案1】:

将表格视图分隔符插入添加为零。不仅仅适用于表格视图单元格。

将此代码添加到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;
}

查看预览:

【讨论】:

  • 好的,然后等待另一个答案@AlexanderFarber
  • 太棒了!很遗憾没有诺贝尔奖来解决愚蠢的 iOS 行为。当然,这种解决方案是有代价的:开发人员可能有其他原因不想想要更改单元边距。但是有一些方法可以解决这个问题(添加一个有自己的边距的子视图)。
  • 只是要明确一点:不需要涉及表格。在cellForRowAtIndexPath: 中设置三件事:(1)单元格的分隔符插入; (2) 单元格的布局边距; (3) 单元格的preservesSuperviewLayoutMargins。这样就可以了。
  • @AshishKakkad 感谢您的回答。但是,在这样做之后,又会留下一些空间。有什么方法可以覆盖这些吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2015-08-04
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多