UITableViewCellStyleSubtitle单元格的分隔线未占用整个宽度 [英] Separator lines for UITableViewCellStyleSubtitle cells not taking the full width

查看:84
本文介绍了UITableViewCellStyleSubtitle单元格的分隔线未占用整个宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已为我的问题准备了)不适应细胞和三编辑以增加行高,但这没有帮助。



此外,我还尝试将自定义插入设置为自定义并且为0像素(表视图 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 - 表视图数据源
- (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:@MyCellforIndexPath :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];

返回单元格;
}

更新:此答案的源代码包含最后帮助我:
iOS 8 UITableView分隔符插入0不工作



   - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
//删除分隔符插入
if([cell respondsToSelector:@selector(setSeparatorInset :)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}

//阻止单元格继承表格视图的边距设置
if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins :)]){
[cell setPreservesSuperviewLayoutMargins:NO];
}

//显式设置单元格的布局边距
if([cell respondsToSelector:@selector(setLayoutMargins :)]){
[cell setLayoutMargins:UIEdgeInsetsZero] ;
}
}

/ *
- (无效)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];

//强制你的tableview边距(这可能是一个坏主意)
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: @MyCellforIndexPath: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];

返回单元格;
}

查看预览:




I have prepared a simple test project for my question at GitHub.

When using UITableViewCellStyleSubtitle type of cells (called Subtitle in Xcode Interface Builder) - for some reason the horizontal lines are not reaching the left side of the screen:

At the above screenshot you can see that the separator lines in "empty cells" are occupying the whole width just fine.

First I thought that the icons (size: 46 x 46 pixels) were not fitting into the cells and tried to increase the row height, but this does not help.

Also I have tried to set Custom Insets to Custom and there Left to 0 pixels (both for Table View and for MyCell), but there is still a gap on the left side:

Here the source code of my 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;
}

UPDATE: The source code from this answer has helped me at the end: 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];
    }
} 
*/

解决方案

Add Table View Separator Insets to Zero. Not Just for Table View Cell.

Add this code in cellForRowAtIndexPath:

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

Integration with your code :

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

See the preview :

这篇关于UITableViewCellStyleSubtitle单元格的分隔线未占用整个宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆