Swift 带有要使用单个数组的节的表视图
我有一个表,其中包含一些项目和这些项目的部分。项目和部分都是硬编码的,我在理解如何从一个数组而不是两个数组加载所有内容时遇到了一些困难 这是我的密码:Swift 带有要使用单个数组的节的表视图,swift,Swift,我有一个表,其中包含一些项目和这些项目的部分。项目和部分都是硬编码的,我在理解如何从一个数组而不是两个数组加载所有内容时遇到了一些困难 这是我的密码: import UIKit class Beta: UIViewController, UITableViewDataSource, UITableViewDelegate{ @IBOutlet weak var tableView: UITableView! let section = ["Fruits", "Veget
import UIKit
class Beta: UIViewController, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var tableView: UITableView!
let section = ["Fruits", "Vegetables"]
let items = [["Apple", "Banana"], ["Carrots", "Broccoli"]]
override func viewDidLoad(){
super.viewDidLoad()
self.tableView.allowsMultipleSelection = true
}
override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.items[section].count
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.section.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return self.section[section]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.items[indexPath.section][indexPath.row]
//MARK: -Checkmark and save support.
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none // to prevent cells from being "highlighted"
return cell
}
}
}
我使用了我能在网上找到的代码,我能找到的唯一工作代码是将部分和项目分成两个数组
如何让它读取这个数组中的所有内容
var Food:NSDictionary = [
//:Section Title : Items
"Fruits" : ["Apple","Banana"],
"Vegetables" : ["Carrots","Broccoli"]
]
你很接近,但是你错误地使用了一本没有顺序的字典。你需要一个数组。它可以是一个一项字典数组,甚至是元组数组:
let arr : [(String, [String])] = ...
更好的是,创建一个自定义结构并使用该结构的数组:
struct Model {
let section : String
let rows : [String]
}
现在,您的数据是[Model]
试试这个-
struct Section {
var name: String!
var items: [String]!
init(name: String, items: [String]) {
self.name = name
self.items = items
}
}
import UIKit
class Beta: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tblView: UITableView!
var sections = [Section]()
override func viewDidLoad() {
super.viewDidLoad()
self.tblView.estimatedRowHeight = 56.0
self.tblView.rowHeight = UITableViewAutomaticDimension
self.tblView.tableFooterView = UIView()
// Initialize the sections array
sections = [
Section(name: "Fruits", items: ["Apple", "Banana"]),
Section(name: "Vegetables", items: ["Carrots", "Broccoli"]),
]
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// For section 1, the total count is items count plus the number of headers
var count = sections.count
for section in sections {
count += section.items.count
}
return count
}
// Add two prototype cell in your storyboard and give identifier "header" for header cell and "cell" for another one.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell?
// Calculate the real section index and row index
let section = getSectionIndex(row: indexPath.row)
let row = getRowIndex(row: indexPath.row)
if row == 0 {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "header")! as UITableViewCell
headerCell.textLabel?.numberOfLines = 0
headerCell.textLabel?.textColor = UIColor.blue
headerCell.textLabel?.lineBreakMode = .byWordWrapping
headerCell.textLabel?.text = sections[section].name
headerCell.selectionStyle = .none
return headerCell
} else {
if cell == nil {
cell = tableView.dequeueReusableCell(withIdentifier: "cell")
}
cell?.textLabel?.numberOfLines = 0
cell?.textLabel?.lineBreakMode = .byWordWrapping
cell?.selectionStyle = .none
cell?.textLabel?.text = sections[section].items[row - 1]
}
return cell!
}
//
// MARK: - Helper Functions
//
func getSectionIndex(row: NSInteger) -> Int {
let indices = getHeaderIndices()
for i in 0..<indices.count {
if i == indices.count - 1 || row < indices[i + 1] {
return i
}
}
return -1
}
func getRowIndex(row: NSInteger) -> Int {
var index = row
let indices = getHeaderIndices()
for i in 0..<indices.count {
if i == indices.count - 1 || row < indices[i + 1] {
index -= indices[i]
break
}
}
return index
}
func getHeaderIndices() -> [Int] {
var index = 0
var indices: [Int] = []
for section in self.sections {
indices.append(index)
index += section.items.count + 1
}
return indices
}
}
struct节{
变量名称:字符串!
变量项:[字符串]!
初始化(名称:String,项:[String]){
self.name=名称
self.items=项目
}
}
导入UIKit
Beta类:UIViewController、UITableViewDataSource、UITableViewDelegate{
@IBLView:UITableView!
var sections=[Section]()
重写func viewDidLoad(){
super.viewDidLoad()
self.tblView.estimatedRowHeight=56.0
self.tblView.rowHeight=UITableViewAutomaticDimension
self.tblView.tableFooterView=UIView()
//初始化节数组
章节=[
章节(名称:“水果”,项目:[“苹果”,“香蕉]),
部门(名称:“蔬菜”,项目:[“胡萝卜”、“西兰花]),
]
}
func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
//对于第1节,总计数是items count加上页眉数
var count=sections.count
一节接一节{
计数+=section.items.count
}
返回计数
}
//在故事板中添加两个原型单元,并为标题单元指定标识符“header”,为另一个单元指定标识符“cell”。
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
变量单元格:UITableViewCell?
//计算实际节索引和行索引
let section=getSectionIndex(行:indexPath.row)
let row=getRowIndex(行:indexath.row)
如果行==0{
让headerCell=tableView.dequeueReusableCell(标识符为:“header”)!作为UITableViewCell
headerCell.textLabel?.numberOfLines=0
headerCell.textLabel?.textColor=UIColor.blue
headerCell.textLabel?.lineBreakMode=.byWordWrapping
headerCell.textLabel?.text=节[section]。名称
headerCell.selectionStyle=.none
回程顶盖
}否则{
如果单元格==nil{
cell=tableView.dequeueReusableCell(带有标识符:“cell”)
}
单元格?.textLabel?.numberOfLines=0
单元格?.textLabel?.lineBreakMode=.byWordWrapping
单元格?.selectionStyle=.none
单元格?.textLabel?.text=节[section]。项[row-1]
}
返回单元!
}
//
//MARK:-辅助函数
//
func getSectionIndex(行:NSInteger)->Int{
let index=getHeaderIndices()
对于0..Int中的i{
var指数=行
let index=getHeaderIndices()
对于0中的i..[Int]{
var指数=0
风险值指数:[Int]=[]
对于self.sections中的节{
索引。追加(索引)
索引+=section.items.count+1
}
回报指数
}
}