繁体   English   中英

iOS UITableView单元格的自定义分隔符不会更改其横向宽度

[英]iOS UITableView cell's custom separator doesn't change its width on landscape

我有UITableView并设置了单元格的分隔符。 一切似乎都很好,但我的分隔符没有改变其在景观上的宽度。 外观如下: 在此处输入图片说明

这是我创建自定义单元格的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.backgroundColor = UIColor.clearColor;
    }
   //set cell's image and texts
   ... 


    UIView *separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)];
    separatorLineView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:0.1];
    [cell.contentView addSubview:separatorLineView];
    [cell layoutIfNeeded];
    return cell;
}

有什么想法,我该如何解决这个问题?

在ListCell.h-中添加分隔线属性

@property (strong, nonatomic)  UIView *separatorLineView;

尝试用以下代码替换分隔线的代码-

int screenHeight;
if ( [[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationPortrait
    &&  [[UIApplication sharedApplication ]statusBarOrientation] != UIInterfaceOrientationPortraitUpsideDown){
    screenHeight = [UIScreen mainScreen].applicationFrame.size.width;
} else {

    screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
}

if(cell.separatorLineView.superview == NULL){
     cell.separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenHeight, 1)];
    cell.separatorLineView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:0.1];
    [cell.contentView addSubview:cell.separatorLineView];
    [cell layoutIfNeeded];
}

我一定会工作!

或者,您可以按故事板添加分隔线-

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM