快速Xcode中的部分和单元格

问题描述 投票:0回答:1

我是一名新手应用程序开发人员,无法在我的应用程序中显示多个部分和单元格。显示2个部分,第一个部分的3个单元格和第二个部分的2个单元格的最佳方法是什么?

swift cell
1个回答
0
投票

仅处理numberOfRowsInSection-

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


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

       return 2

   }

0
投票

您将要做类似的事情。只要确保将YourCellIdentifier和YourCellClass替换为您拥有的任何东西即可。

class TableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

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

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

        // configure your cell here

        return cell
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.