将Table View中的部分添加到现有列表中?

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

是否可以在现有列表中添加部分?或者我可以以某种方式硬编码吗?因此第3行和第4行由一个部分分隔,第9行和第10行由一个部分分开,依此类推 我试图添加部分,但这不是很成功:

列表文件:

import Foundation

class ListItem {
    var section = ""
    var listItem = ""
    var description = ""
    var extraInfo = ""
    var counter: Int = 0

    init(section: String, listItem: String, description: String, ekstraInfo: String) {
        self.section = section
        self.listItem = listItem
        self. description = description
        self.ekstraInfo = ekstraInfo
    }
}    

查看控制器:

 let staticList: [ListItem] =
    [

        ListItem(section: "Section 1", listItem: "Bananas", description: "Yellow", ekstraInfo: "Bent"),
        ListItem(section: "Section 2", listItem: "Apples", description: "Red", ekstraInfo: "Round"),
        ListItem(section: "Section 3", listItem: "Strawberries", description: "Red", ekstraInfo: ""),
        ListItem(section: "Section 4", listItem: "Carrots", description: "Orange", ekstraInfo: ""),
        ListItem(section: "Section 5", listItem: "Lime", description: "Green", ekstraInfo: "Round"),
    ]    

 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
    {
        if (tableView == MainTableView)
        {
            return staticList[section].section
        }else
        {
            return  nil
        }
    }    

编辑:

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

            if let customCell = cell as? MenuCell
            {
                let itemIndex = indexPath.row
                let listItem = staticList[itemIndex]

                customCell.itemLabel.text = listItem.listItem
                customCell.descriptionLabel.text = listItem.description
                customCell.exstraInfoLabel.text = listItem.exstraInfo
                customCell.counterLabel.text = "\(listItem.counter)"

                customCell.delegate = self


            }
            return cell

        }
    }
swift uitableview sections
1个回答
1
投票

我将与一些硬编码部分分享一个例子。这应该可以帮助您了解它的工作原理。

let numberOfRows = [2, 3, 1, 4, 5]

这里我们有一个整数数组,表示行数。基本上是5个部分,每个部分分别有2,3和5行

将以下内容添加到UITableViewDataSource

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return numberOfRows[section]
}

这应该给你一个UITableView有5个部分,每个部分分别有2,3,1,4,5行。

numberOfRows以获得更多部分,更多行等。

编辑:

每个部分加载相同单元格的原因是因为staticList是一个单维数组。因此,在每个部分中,相同的行不断被提取,因为indexPath.row从每个部分的0开始。为了解决这个问题,使staticList成为一个二维数组。这是如何做...

let staticList: [[ListItem]] = [
    [
        ListItem(section: "Section 1", listItem: "Bananas", description: "Yellow", ekstraInfo: "Bent"),
        ListItem(section: "Section 1", listItem: "Apples", description: "Red", ekstraInfo: "Round")
    ],
    [
        ListItem(section: "Section 2", listItem: "Strawberries", description: "Red", ekstraInfo: "")
    ],
    [
        ListItem(section: "Section 3", listItem: "Carrots", description: "Orange", ekstraInfo: ""),
        ListItem(section: "Section 3", listItem: "Lime", description: "Green", ekstraInfo: "Round")
    ]
]

现在staticList有3个部分,每个部分分别有2个,1个,2个ListItems。

最后,对cellForRowAtIndexPath函数进行一些小改动......

// let itemIndex = indexPath.row
// let listItem = staticList[itemIndex]

let listItem = staticList[indexPath.section][indexPath.row]

顺便说一句,你可以从section删除ListItem属性,以使事情更清洁。离开它不应该破坏任何东西。

© www.soinside.com 2019 - 2024. All rights reserved.