UITABLE视图部分,每个部分中具有不同的原型单元

时间:2018-12-12 05:02:04

标签: swift

我有一个包含不同部分的表格视图,并且对于每个部分,它都有不同的原型单元。我已经在情节提要中创建了原型单元,但是在cellForRowAtIndexPath中不能创建多个单元。我已附上整个代码,以供参考。

这是我的故事板设计

Screen Shot

这是最终的输出屏幕,看起来应该像

Screen Shot

    import UIKit

    class PlacePeopleViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

        @IBOutlet weak var friendlistTableView: UITableView!

        var sectionHead = ["Friends", "Others"]

        override func viewDidLoad() {
            super.viewDidLoad()
            friendlistTableView.delegate = self
            friendlistTableView.dataSource = self

            // Do any additional setup after loading the view.
        }


        // MARK: - tableview datasource & delegate

        func numberOfSections(in tableView: UITableView) -> Int {
            return sectionHead.count
        }

        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return self.sectionHead[section]
        }


        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 0 {
                return 5
            }else {
                return 10
            }
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! RecentVizzTableViewCell
            cell.placeName.text = "poland"
            cell.placeAddress.text = "Simon Albania"


            return cell
        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 79
        }

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

    }

1 个答案:

答案 0 :(得分:2)

  

注意:您可以仅使用一个单元格来执行此操作。只需通过检查某些条件来隐藏/显示 connect 按钮。

如果您要在不同部分中使用不同的单元格,请在cellForRowAt indexPath方法内,根据以下部分返回单元格。

switch  indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! RecentVizzTableViewCellOne
            return cell
        default:
            let defaultcell = tableView.dequeueReusableCell(withIdentifier: "defaultcell") as! RecentVizzTableViewCellDefatult
            return defaultcell
  }

希望这对您有帮助。祝你好运。