我试图使每个tableViewCell具有不同的高度。我尝试声明每个单元格标识符及其名称,并使用“ switch”将其分为大小写。实际上,我也想在代码中的mainViewCell上指定一个标签。如何更改每个单元格的高度?

 import UIKit

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var tableView: UITableView!
    override func viewDidLoad() {
            super.viewdidload()
        tableView.registerNib(UINib(nibName: "mainViewTableViewCell", bundle: nil), forCellReuseIdentifier: "mainViewCell")
}

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.scrollEnabled = false
        return 4
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        switch tableView.numberOfRowsInSection(4) {
            case 0:
                let titleCell = tableView.dequeueReusableCellWithIdentifier("titleCell")! as UITableViewCell
            return titleCell
            case 1:
                let wayCell = tableView.dequeueReusableCellWithIdentifier("whichWayCell")! as UITableViewCell
                return wayCell
            case 2:
                let campusCell = tableView.dequeueReusableCellWithIdentifier("campusCell")! as UITableViewCell
                return campusCell
            default :
                    let mainViewCell = tableView.dequeueReusableCellWithIdentifier("mainViewCell")! as! mainViewTableViewCell
                    mainViewCell.mainViewLabel.text = onUpdate(timer)
                    return mainViewCell

        }
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switch tableView.numberOfRowsInSection(4) {
    case 0:
        tableView.numberOfRowsInSection(1)
    return 50
    case 1:
        tableView.numberOfRowsInSection(2)
    return 50
    case 2:
        tableView.numberOfRowsInSection(3)
    return 50
    default:
        tableView.numberOfRowsInSection(4)
    return 300
    }
}

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}

最佳答案

您应该使用传递到UITableViewDataSource方法中的indexPath。您的代码有几个问题。有文档here

您的cellForRowAtIndexPath方法中的switch tableView.numberOfRowsInSection(4)语句应为:switch(indexPath.section)

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switch tableView.numberOfRowsInSection(4) {
    case 0:
        tableView.numberOfRowsInSection(1)
    return 50
    case 1:
        tableView.numberOfRowsInSection(2)
    return 50
    case 2:
        tableView.numberOfRowsInSection(3)
    return 50
    default:
        tableView.numberOfRowsInSection(4)
    return 300
}


为什么要调用numberOfRowsInSection()?,这应该留给OS来调用。尝试这个。如果您要更改的表格单元格高度因截面而异,则将switch(indexPath.row)更改为switch(indexPath.section)

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

    var height : CGFloat = 0

    switch(indexPath.row) {
    case 0, 2, 3:
        height = 50.0
    case 4:
        height = 300.0
    default:
         break
    }

    return height

}

关于ios - 如何制作一个自定义的TableViewCell,使它们每个都有不同的高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34381603/

10-11 02:40