UiTableViewCell动态高度适用于iphone而不是ipad尺寸类

时间:2015-07-23 16:28:57

标签: swift uitableview ios8 autolayout

所以基本上我有任何我用于iPhone的常规尺寸类和iPad的常规尺寸。话虽如此,我有一个自定义UITableViewCell,我有iPhone的单元格自动调整大小。我垂直布局了约束,以帮助它知道细胞的大小。

问题在于iPad尺寸类我做同样的事情,显然布局有点不同但我检查了每个元素,它有一个顶部和底部以及高度约束,但UITableViewCell是和iPhone布局一样高。

以下是该场景的代码:

import UIKit

class TruckListMainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var truckNames = ["This Truck Test"]
var truckImages = ["default-truck.png"]
var oneAverages = ["5.02"]
var twoAverages = ["6.02"]
var threeAverages = ["7.02"]
var fourAverages = ["8.02"]
var oneLabels = ["30 Day MPG"]
var twoLabels = ["60 Day MPG"]
var threeLabels = ["90 Day MPG"]
var fourLabels = ["Lifetime MPG"]

@IBOutlet weak var truckListTable: UITableView!
@IBOutlet weak var openMenu: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.truckListTable.rowHeight = UITableViewAutomaticDimension
    self.truckListTable.estimatedRowHeight = 200.0

    openMenu.target = self.revealViewController()
    openMenu.action = Selector("rightRevealToggle:")

    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return truckNames.count

}

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

    // Configure the cell...
    cell.truckName.text = truckNames[indexPath.row]
    cell.truckImage.image = UIImage(named: truckImages[indexPath.row])
    cell.averageOne.setTitle(oneAverages[indexPath.row], forState: UIControlState.Normal)
    cell.averageTwo.setTitle(twoAverages[indexPath.row], forState: UIControlState.Normal)
    cell.averageOne.setTitle(threeAverages[indexPath.row], forState: UIControlState.Normal)
    cell.averageOne.setTitle(fourAverages[indexPath.row], forState: UIControlState.Normal)
    cell.labelOne.setTitle(oneLabels[indexPath.row], forState: UIControlState.Normal)
    cell.labelTwo.setTitle(twoLabels[indexPath.row], forState: UIControlState.Normal)
    cell.labelThree.setTitle(threeLabels[indexPath.row], forState: UIControlState.Normal)
    cell.labelFour.setTitle(fourLabels[indexPath.row], forState: UIControlState.Normal)
    return cell
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    cell.backgroundColor = UIColor.clearColor()

    if indexPath.row % 2 == 0 {

        cell.backgroundColor = UIColor(red: 202/255.0, green: 202/255.0, blue: 202/255.0, alpha: 1)
    } else {

        cell.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1)
    }
}




/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

任何帮助将不胜感激,我不会在这里发布,但我已经在这个问题上苦苦挣扎了将近2天。如果我能发布图片,我知道这会有助于看到约束。

1 个答案:

答案 0 :(得分:0)

  

我检查了每个元素,它有一个顶部和底部以及高度约束,但UITableViewCell与iPhone布局一样高

您可能会对如何确定细胞高度感到困惑。如果您不希望由内部约束确定单元格高度,则需要将表视图的rowHeight属性设置为某个实际数字( not UITableViewAutomaticDimension)。