具有两个原型单元的 UITableView

Mat*_*tty 1 uitableview ios swift

更新 - 感谢 Farhad 回答我最初的帖子。这篇文章的底部是我遇到的关于动态单元高度自动尺寸标注的新问题

我尝试过搜索其他帖子,但没有成功。

正如标题所示,我想让我的表格视图显示两个单元格,第二个单元格是自定义的。

第一个单元格有 4 行 - OD、重量、ID、等级。

第二个单元格(自定义单元格)将有 8 个标签,显示第一个单元格输入的结果。

我有两个单元格的重用标识符。我将第一个单元称为“capacitiesCell”,将第二个单元称为“cell2”

对于第二个单元格,我创建了一个名为“CapacitiesTableViewCell”的新 tableViewCell 文件,其中 8 个标签中有 4 个作为出口。

旁注:对于单元格的名称,我现在只是放置了一个通用的“第#节,第#行”,但是cell.textlabel?.text = capacityID当我解决问题时会将代码行更改...假设这是正确的方法去做这件事吧。

到目前为止,这是我的代码:

class CapacitiesTableViewController: UITableViewController {

let capacityParameters = ["O.D", "Weight", "I.D", "Grade"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section == 0 {
        return capacityParameters.count
    } else {
        return 1
    }

}


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

    //let capacityID = capacityParameters[indexPath.row]
    cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"

    return cell

    let dimensionAndStrengthCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)
    // my code goes here
    return dimensionAndStrengthCell
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    if section == 0 {
        return "Casing Specification"
    } else {
        return "Dimension & Strength"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的故事板: 在此输入图像描述

但是,当我运行该应用程序时,我得到以下信息: 在此输入图像描述

更改 CELL2 的单元高度

所以在我看来DidLoad我有以下内容:

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 200.0
}
Run Code Online (Sandbox Code Playgroud)

我试图让我的视图看起来像下面的屏幕截图,但“强度和尺寸”部分的单元格高度停留在默认高度。 在此输入图像描述

我从 Farhad 的回复中添加的新代码是:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // init return type of cell
    var returnCell: UITableViewCell!

    if indexPath.section == 0 { // you can also check for section with section.index
        returnCell = tableView.dequeueReusableCellWithIdentifier("capacitiesCell", forIndexPath: indexPath)

        //let capacityID = capacityParameters[indexPath.row]
        returnCell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"

    return returnCell

    } else {

        returnCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 200.0

        return returnCell

    }
}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    if section == 0 {
        return "Casing Specification"
    } else {
        return "Dimension & Strength"
    }
}
Run Code Online (Sandbox Code Playgroud)

Far*_*had 5

在您的文件中cellForRowAtIndexPath,您检查需要在哪个位置输入新的rowsectionwithif/else语句。

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        //Init the Return of Cell Type
        var returnCell: UITableViewCell!

        if index.row == 0 { // You can also check for section index.section
         returnCell = tableView.dequeueReusableCellWithIdentifier("capacitiesCell", forIndexPath: indexPath)

        //let capacityID = capacityParameters[indexPath.row]
        cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"

        return returnCell

        } else {

        returnCell = //Other cell at other rows or section
        return returnCell

        }

    }
Run Code Online (Sandbox Code Playgroud)

  • 法哈德感谢您的帮助。我对处理约束的方式做了一些改变,它似乎奏效了。非常感谢您抽出时间! (2认同)