UITableView 缺少一些分隔线

2024-01-05

我正在使用代码构建一个 uitableview,如下所示:

UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 290, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 290)]; 
tableView.delegate = self;
tableView.dataSource = self;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"settingtabcell"];

以及数据资源和视图委托

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"settingtabcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }else{
        for(UIView *subview in cell.subviews){
            [subview removeFromSuperview];
        }
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(70, 0, [UIScreen mainScreen].bounds.size.width - 20, 90)];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 30, 32, 32)];
    
    switch (indexPath.row) {
        case 0:
            label.text = @"Copyright information";
            imageView.image = [UIImage imageNamed:@"copyright.png"];
            break;
        case 1:
            label.text = @"Clear cache";
            imageView.image = [UIImage imageNamed:@"clearcache.png"];
            break;
        case 2:
            label.text = @"Feedback";
            imageView.image = [UIImage imageNamed:@"feedback.png"];
            break;
        case 3:
            label.text = @"About us";
            imageView.image = [UIImage imageNamed:@"about.png"];
            
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell addSubview:imageView];
    [cell addSubview:label];
    return cell;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

问题是有些分隔线丢失了,有些则没有,这真的让我很困惑。

那么是什么原因导致了这个问题呢?

EDIT

对于那些可能关心的人来说,这个屏幕截图不是在模拟器上,而是在 ihpone 设备上捕获的。

这条线原本是显示的,只是向下滚动并弹回来后,这条线就不见了。


看来您正在使用自定义单元格类。 如果您要覆盖layoutSubviews()单元类实现中的方法,确保在该方法中具有以下内容:

super.layoutSubviews()

或者在 Objective-C 中:

[super layoutSubviews];

这为我解决了问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UITableView 缺少一些分隔线 的相关文章

随机推荐