ios - UITableView - 具有相同数组的部分中的不同行

标签 ios uitableview swift

我有一个数组,我从后端获取它的值,我有一个包含 3 个部分和不同行的 UITableView:

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 3
}

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

    if section == 1 {

        return 2
    } 
    else if section == 2 {

        return 1

    } else {

        return 3
    }

}




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


    let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell

    // Configure the cell...

    if indexPath.section == 1 {

        myCell.titleLabel.text = titles[indexPath.row + 3]

    }
    else if indexPath.section == 2 {

        myCell.textLabel?.text = "Section 2"

    }
    else {


        myCell.titleLabel.text = titles[indexPath.row] // ERROR!

    }

    return myCell
}


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

    return "Section"
}

我需要让来自 2 个不同部分的单元格从同一个数组中获取它们的数据,并且我在中间有一个部分应该从不同的数组中获取数据。当我运行时出现此错误:

fatal error: Array index out of range

我的数组包含 5 个值,它是这样的:

 titles = ["1", "2", "3", "4", "5"]

PS:我的 TableView 有自定义单元格

编辑:我编辑了部分的排列(我第一次知道它们是从下到上编入索引的),但我最后一个部分的单元格仍然出错,而且当我向下滚动时还有一个非常奇怪的行为备份,第 2 节替换第 1 节的第 3 个单元格,它被移动到表格的底部!

最佳答案

您不能像现在这样使用循环,因为这只会为该部分中的每个单元格提供您循环通过的最后一个索引(索引 2 或 4)中的值。通常,对于分区 TableView ,您将使用一个数组数组来填充这些部分。如果您想使用单个数组,并且每个部分中的行数与您显示的一样,那么以下应该有效,

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var titles = ["1", "2", "3", "4", "5"]

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

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

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

        if section == 0 {

            return 3
        }
        else if section == 1 {

            return 1

        } else {

            return 2
        }
    }

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

        let myCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

        if indexPath.section == 0 {
            myCell.textLabel!.text = titles[indexPath.row]
        }
        else if indexPath.section == 1 {
            myCell.textLabel!.text = "Section 2"
        }
        else {
            myCell.textLabel!.text = titles[indexPath.row + 3]
        }

        return myCell
    }

}

这为六个单元格提供了 1、2、3、“第 2 节”、4、5(我为测试用例使用了标准的 UITableViewCell)。

关于ios - UITableView - 具有相同数组的部分中的不同行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631559/

相关文章:

ios - 如何从后台ios打开应用程序

arrays - 餐车被覆盖

ios - UITableView 和 UICollectionView 在一行中重用第一行中的集合

ios - 使用导航 Controller 将 View 复制到项目中 - "Image View"的框架在运行时会有所不同

ios - PFUser 上的 PFQuery 返回 nil

ios - 当我试图从另一个 View Controller (Objective-c?

ios - Swift:如何在按下按钮时遍历所有 tableView 行

ios - 如何让两个不同的表格 View 单元格始终成对显示在同一个表格 View 中?

swift - 当标准位置服务和重大位置变化一起使用时,会发生什么?

swift - Facebook AppInvites 与 Branch.io 在 Swift 中的示例