UITableViewCell具有不同的屏幕尺寸

时间:2015-08-24 20:51:07

标签: ios swift uitableview

当我在模拟器中运行我的应用程序时,我的表格单元格是正确的长度(iPhone 5s)但是当我在iPhone6上运行它时,单元格开始作为整个屏幕宽度但是然后转回到iPhone 5s大小

关于我做错的任何想法?

代码:

override func viewDidLoad() {
    super.viewDidLoad()

    theTableView.setTranslatesAutoresizingMaskIntoConstraints(false) //Don't forget this line

    var leftSideConstraint = NSLayoutConstraint(item: theTableView, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1.0, constant: 0.0)
    var rightSideConstraint = NSLayoutConstraint(item: theTableView, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: 0.0)

    view.addConstraints([leftSideConstraint, rightSideConstraint])

  }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MySampleCell") as! UITableViewCell


    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let screenWidth = screenSize.size.width
    let screenHeight = screenSize.size.height

    theTableView.frame = CGRectMake(0, 0, screenWidth, screenHeight)
    cell.frame = CGRectMake(0, 0, screenWidth - 20, 44)
    return cell
 }

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

theTableView.frame = CGRectMake(0, 0, screenWidth, screenHeight)

视图的框架可能在viewDidLoad中配置,通常不会更改。

cell.frame = CGRectMake(0, 0, screenWidth - 20, 44)

如果您只想将单元格的高度设置为44,请设置tableView.estimatedRowHeight = 44.0或将以下覆盖添加到视图控制器:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return CGFloat(44)
}

答案 1 :(得分:0)

在介绍320px以上的屏幕之前,我已经处理了这段时间。

UITableViewCell子类,并确保覆盖

- (void)layoutSubviews

在那里调用super.layoutSubviews()然后读取帧大小并根据新的帧大小调整其中的任何子元素。

答案 2 :(得分:0)

似乎问题可能与实际的UITableView有关。您使用的是UITableViewController,还是在视图中添加了表格?
要使您的视图适应,您不应在代码中使用显式大小。如果手动添加,请设置tableView的顶部,底部,前导和尾部约束,并在您创建的自定义单元格中执行相同操作。

如果您有任何疑问或有任何疑问,请与我们联系。

相关问题