如何以分组方式包含两个内容:动态和静态
How to have, in a grouped style, two contents : dynamic and static
我知道有这个话题:“Mixing static and dynamic sections in a grouped table view”
可是过了好几个小时,我还是没看完这篇文章"project" :
为此:
click there to see a screen capture
(这是两组样式:在第一组中,单元格采用 dynamic 样式,另一组单元格采用 static 样式。)
所以,我有一个列表:
var listOfWords = ["Cat", "Dog", "Horse", "Dolfin", "Robot"]
我会在每个单元格中显示每个单词 -> 所以它是动态的...
然后在另一组中,向下我想要一个 "cell-button" 如果我单击它,我可以创建将添加到列表中的其他词。
所以如果有人能帮助我,那将会很有帮助!
(请发送代码;-))
将按钮放在 tableFooterView 中。
已编辑
您需要有两个部分并在 cellForRowAtIndexPath
数据源方法上以不同方式管理它们。
第一个
在 numberOfSections
数据源方法上添加两个部分
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
第二
管理 numberOfCell
数据源方法
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return yourArray.count
case 1:
return 1
default:
return 0
}
}
第三
相应地管理每个部分。根据您所说的,您希望第一部分是动态的,第二部分是静态的。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
// your dynamic cells
case 0:
let dynamicCell = tableView.dequeueReusableCell(withIdentifier: "dynamicCell") != nil ? tableView.dequeueReusableCell(withIdentifier: "dynamicCell")! : UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "dynamicCell")
dynamicCell.textLabel?.text = yourArray[indexPath.row]
return dynamicCell
// your static cell
case 1:
guard let staticCell = tableView.dequeueReusableCell(withIdentifier: "staticCell") else {
let staticCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "staticCell")
staticCell.textLabel?.text = "Static"
return staticCell
}
staticCell.textLabel?.text = "Static"
return staticCell
default:
return UITableViewCell()
}
}
希望这对您有所帮助。
此外,我的数据源方法名称可能存在一些小错误
我知道有这个话题:“Mixing static and dynamic sections in a grouped table view”
可是过了好几个小时,我还是没看完这篇文章"project" :
为此: click there to see a screen capture (这是两组样式:在第一组中,单元格采用 dynamic 样式,另一组单元格采用 static 样式。)
所以,我有一个列表:
var listOfWords = ["Cat", "Dog", "Horse", "Dolfin", "Robot"]
我会在每个单元格中显示每个单词 -> 所以它是动态的...
然后在另一组中,向下我想要一个 "cell-button" 如果我单击它,我可以创建将添加到列表中的其他词。
所以如果有人能帮助我,那将会很有帮助! (请发送代码;-))
将按钮放在 tableFooterView 中。
已编辑
您需要有两个部分并在 cellForRowAtIndexPath
数据源方法上以不同方式管理它们。
第一个
在 numberOfSections
数据源方法上添加两个部分
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
第二
管理 numberOfCell
数据源方法
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return yourArray.count
case 1:
return 1
default:
return 0
}
}
第三
相应地管理每个部分。根据您所说的,您希望第一部分是动态的,第二部分是静态的。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
// your dynamic cells
case 0:
let dynamicCell = tableView.dequeueReusableCell(withIdentifier: "dynamicCell") != nil ? tableView.dequeueReusableCell(withIdentifier: "dynamicCell")! : UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "dynamicCell")
dynamicCell.textLabel?.text = yourArray[indexPath.row]
return dynamicCell
// your static cell
case 1:
guard let staticCell = tableView.dequeueReusableCell(withIdentifier: "staticCell") else {
let staticCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "staticCell")
staticCell.textLabel?.text = "Static"
return staticCell
}
staticCell.textLabel?.text = "Static"
return staticCell
default:
return UITableViewCell()
}
}
希望这对您有所帮助。
此外,我的数据源方法名称可能存在一些小错误