UITableViewCell 宽度未根据 ios 设备类型动态调整

UITableViewCell width is not adjusting dynamically based on ios device type

我的 tableview 单元格完全以编程方式创建(我正在尝试学习在不使用情节提要的情况下从头开始构建应用程序)并且单元格的宽度变得混乱。

这是 http://imgur.com/ki6txqg 电池在 iPhone 6 Plus 中的样子的屏幕截图。我正在尝试设置单元格,以便自动调整单元格 (self.view) 中的 UIView,使其填满整个屏幕。我不确定为什么宽度保持不变。任何建议将不胜感激。

-(instancetype)initWithTweet:(PCRTweet *)tweet reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super init];

if (self) {
    _tweet = tweet;

    reuse = reuseIdentifier;

    CGSize cellSize = self.contentView.frame.size;

    CGRect backgroundView = CGRectMake(10.f, 5.f, (cellSize.width-20.f), (cellSize.height + 90.f));


    self.view = [[UIView alloc] initWithFrame:backgroundView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.layer.cornerRadius = 8;
    self.view.layer.masksToBounds = YES;
    self.view.layer.borderWidth = 1.0f;
    self.view.layer.borderColor = background_color_gray.CGColor;
    self.view.layer.masksToBounds = NO;

    [self.contentView addSubview:self.view];

    CGRect picView = CGRectMake(0.f, 0.f, 65.f, 65.f);

    self.contentView.backgroundColor = background_color_gray;

}

return self;
}

请阅读以下清单中的要点,以确保您做对了:

-[ ] 你检查过你的 contentView 是动态变化的吗?

-[ ] 您是否尝试过以编程方式设置约束?

-[ ] 尝试在最大视图上使用约束:将自动调整相对视图

除此之外,您还可以为您的框架自动调整大小。

您在 table 视图 class

中尝试这两个 UITableView Delegate 方法

对于动态高度

#pragma mark - UITableView Delegates

-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath {

     return 44.0;
}

-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath {

    return UITableViewAutomaticDimension;
}

For Width(获取视图控制器宽度并带到背景视图)

CGSize viewWidth = self.contentView.superview.superview.superview.superview.frame.size;

// self.contentView.superview  -> return UITableViewCell
// self.contentView.superview.superview  -> return UITableViewWrapperView
// self.contentView.superview.superview.superview  -> return UITableView
// self.contentView.superview.superview.superview.superview  -> return View Controller

CGRect backgroundView = CGRectMake(10.f, 5.f, (viewWidth.width-20.f), (cellSize.height + 90.f));

尝试为您的视图使用自动调整大小。

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;