【问题标题】:Use different type of cells and different number of rows in UITableViewController在 UITableViewController 中使用不同类型的单元格和不同的行数
【发布时间】:2017-12-01 07:11:33
【问题描述】:

我正在尝试使用具有 4 个部分的 UITableViewController(静态表格视图),每个部分都有不同类型的单元格。

部分中的单元格数量取决于从 Web 服务接收到的数据。在情节提要中,我在每个部分中创建了一种类型的单元格。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Number of rows in attachment sect
    if section == 2 {
        guard let currentDataModel = dataModel else { return 0 }
        return currentDataModel.attachments.count
    }
    else if section == 3 {
        guard let currentDataModel = dataModel else { return 0 }
        return currentDataModel.contacts.count
    }
    return super.tableView(tableView, numberOfRowsInSection: section)
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let currentSection = indexPath.section
    if currentSection == 2 {

        let currentRow = indexPath.row
        let cell = tableView.dequeueReusableCell(withIdentifier: "attachmentCell", for: indexPath) as! AttachmentViewCell
        cell.setDataForCell(dataModel: dataModel?.attachments[currentRow], indexPath: indexPath)
        return cell
    }
    //Show cells for Contact Section
    else if currentSection == 3 {
        let currentRow = indexPath.row
        let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactViewCell
        cell.setDataForCell(dataModel: dataModel?.contacts[currentRow], indexPath: indexPath)
        return cell
    }
    return super.tableView(tableView, cellForRowAt: indexPath)
}

我在调用 dequeueReusableCellWithIdentifier:forIndexPath: 时收到此错误:

由于未捕获的异常“NSRangeException”而终止应用,原因:“*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]”

我可以在 UITableViewController 的每个部分中使用不同类型的单元格和不同的行数吗?

【问题讨论】:

  • 请分享一些尝试过的代码,如数组信息对于部分,以及 cellforRow 主代码
  • 错误清楚地表明您正在访问大小为 2 的数组中的第三个元素。检查您在部分中的行数返回正确的行数
  • @Vinodh,是的,它返回该部分中正确的行数。
  • 数组格式似乎是错误的,实际上并没有处理 Sections ,我使用了一个带有部分的 tableView,但如果你愿意,我可以与你分享所有部分的一个单元格,否则需要查看数组格式
  • 崩溃发生在哪一行?

标签: ios swift uitableview


【解决方案1】:

你可以试试这个。如果需要四个部分,然后在numberofsections 中返回 4。您可以将标题添加到每个部分,如下所示,然后将其添加到viewForHeaderInSection 中的标签。您必须正确提供每个部分中的行数。

override func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 4
    }


    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if(section == 0)
        {
            return "Section 0"
        }
        else if(section == 1)
        {
            return "Section 1"
        }
        else if(section == 2)
        {
            return "section 2"
        }
        else if(section == 3)
        {
            return "section 3"
        }
        return ""
    }

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows

        switch(section)
        {
        case 0: //for first section
            //return your count

        case 1://Cells for

            //return your count

        case 2:

            //return your count

        case 3:
          //return your count

        default:
            return 4
        }

    return 0
}

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1

            return cell 

        } 
    else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1

            return cell 

        }

 else if indexPath.section == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1

            return cell 

        }

 else if indexPath.section == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1

            return cell 

        }
else {

            let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? Cell2
            //Set up labels etc.
            return cell!
        }
    }

 override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
    headerView.layer.borderWidth = 2
    let myLabel = UILabel()
    myLabel.frame = CGRectMake(0, 0, tableView.frame.width - 70, 50)
    myLabel.font = UIFont.boldSystemFontOfSize(18)
    myLabel.textColor = UIColor.whiteColor()
    myLabel.textAlignment = .Left
    myLabel.text = "  " + self.tableView(tableView, titleForHeaderInSection: section)!
    headerView.addSubview(myLabel)
    return headerView
}

如果您使用的是 .xib 文件,那么您还应该在 viewDidLoad() 中将该文件注册为

self.tableView.registerNib(UINib(nibName: "cell1", bundle: nil), forCellReuseIdentifier: "cell1")

self.tableView.registerNib(UINib(nibName: "cell2", bundle: nil), forCellReuseIdentifier: "cell2")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多