繁体   English   中英

表格视图中的多个单元格

[英]multiple cell in tableview

我正在使用tableview的ios项目。 我可以正确地填充表格视图,但是现在我希望表格视图中有两个单元格。

  1. 这是一个固定单元格,我可以将datepicker放入其中
  2. 应该用可能来自存储或硬编码的数据填充该数据。

这意味着我希望固定第一个单元格,因为它仅包含日期选择器。

这是到目前为止我实现(2)要求的代码,这意味着我只能实现一个单元。

let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]

 override func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

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

        return animals.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = animals[indexPath.row]

        return cell
    }

任何帮助都会得到应用。 谢谢

let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.reloadData()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 1
    }else{
        return animals.count
    }

}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_IDENTIFIER_FOR_DATEPICKER_CELL", for: indexPath)
        return cell
    }else{
        let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_IDENTIFIER_FOR_DEFAULT_CELL", for: indexPath
        cell.textLabel?.text = animals[indexPath.row]
        return cell
    }
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}

您可以为此划分两个部分::

let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]

 override func numberOfSections(in tableView: UITableView) -> Int {

    return 2 // one for cell 1, and other for 2nd types
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0  { return 1 } // 1 for datePicker
    else { return animals.count } // requirement 2

}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    if indexPath.section == 0 {
       cell.textLabel?.isHidden= true
       var datePicker = ...
       // create date Picker and cell.contentView.addSubview(datePicker)
    } else {
       cell.textLabel?.text = animals[indexPath.row]
    }


    return cell
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM