기존 목록에 섹션을 추가 할 수 있습니까? 아니면 어떻게 든 하드 코딩 할 수 있습니까? 그래서 3 행과 4 섹션으로 구분되며, 행 9, 10 섹션으로 구분되어 있으므로
에 내가 섹션을 추가하려고했으나 이들은 매우 성공하지 :Table View에 섹션을 기존 목록에 추가 하시겠습니까?
목록 파일 :
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
}
}
staticList를'[[ListItem]]'배열로 배열하는 것이 쉬울 수 있습니다. 각 "section"은 ListItem의 배열입니다. –