2016-03-15 58 views
0

我的桌面视图单元格是完全以编程方式创建的(我试图学会从头开始构建应用程序而不使用故事板),单元格的宽度也越来越混乱。UITableViewCell宽度不会动态调整基于iOS设备类型

下面是iPhone 6 Plus中单元格外观的屏幕截图http://imgur.com/ki6txqg。我试图设置单元格,以便单元格中的UIView(self.view)自动调整,以便填充整个屏幕。我不知道为什么宽度保持静态。任何建议将不胜感激。

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

enter image description here

+0

你在tableview上使用约束吗? –

+0

use self.view.frame.size.width-20.f CGRect backgroundView = CGRectMake(10.f,5.f,(self.view.frame.size.width -20.f),(cellSize.height + 90.f)); –

+1

我建议你修改layourSubviews中的框架或使用约束。 – childrenOurFuture

回答

0

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

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
2

请阅读下列清单中的点,以确保你做的一切都是正确的:

- []你检查你的内容查看是动态变化?

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

- []尝试使用上最大的视图的约束:将汽车从它调整相对的观点

除此之外,可以自动调整你的框架。

+0

如何检查contentview是否动态更改? –

+0

由于您没有使用故事板+约束条件,因此您可以尝试简单地打印框架坐标和尺寸..有点怪异的方式,但值得一试。 – djay

+0

当我登录的尺寸,宽度出现为320无论设备类型 –

1

尝试在表视图类

这两UITableView的委托方法对于动态高度

#pragma mark - UITableView Delegates 

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

    return 44.0; 
} 

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

    return UITableViewAutomaticDimension; 
} 

对于宽度(获得视图控制器宽度,并采取backgroundView)

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)); 
+0

的信息。该问题与宽度不高度有关 –

+0

我试着添加CGSize cellSize = self.contentView.superview.frame.size; 但它没有改变任何东西 –

+0

请试试这个 –

相关问题