【发布时间】:2017-05-19 00:51:20
【问题描述】:
如何设置section tableview上选中的单元格的背景颜色
每个部分或流派都应该只有一个选定的单元格enter image description here
请看图片说明
我知道如何在表格视图中选择单个单元格,但在这里我希望每个部分只有一个选择单元格
这是我的代码
var sections = [
Sections(genre: "???? Animation",
movies: ["The Lion King", "The Incredibles"],
expanded: false),
Sections(genre: "???? Superhero",
movies: ["Guardians of the Galaxy", "The Flash", "The Avengers", "The Dark Knight"],
expanded: false),
Sections(genre:"???? Horror",
movies: ["The Walking Dead", "Insidious", "Conjuring"],
expanded: false)
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].movies.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(sections[indexPath.section].expanded){
return 44
}else{
return 0
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = ExpandableHeaderView()
header.customInit(title: sections[section].genre, section: section, delegate: self)
return header
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!
cell.textLabel?.text = sections[indexPath.section].movies[indexPath.row]
return cell
}
////////////
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
//let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!
cell?.contentView.backgroundColor = UIColor.red
//cell?.backgroundColor = UIColor.red
}
func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
//let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!
cell?.contentView.backgroundColor = UIColor.white
//cell?.backgroundColor = UIColor.blue
}
////////////
func toggleSection(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
tableView.beginUpdates()
for i in 0 ..< sections[section].movies.count{
tableView.reloadRows(at: [IndexPath(row: i , section: section)], with: .automatic)
}
tableView.endUpdates()
}
【问题讨论】:
-
所以让我弄清楚这一点......你可能想要第一部分的蓝色......第二部分的红色和第三部分的红色......对吗?
-
不,我不关心颜色我只想在类型部分选择单部电影,如图像描述(见)
标签: ios swift uitableview swift3 uicollectionviewcell