【问题标题】:how to set background color of the cells which are selected on section tableview如何设置节表视图上选择的单元格的背景颜色
【发布时间】: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


【解决方案1】:

你可以做的是完整使用table view提供的delegate函数:

func tableView(UITableView, didSelectRowAt: IndexPath)中可以查看被选中的section和row。您还需要对返回的 var indexPathsForSelectedRows: [IndexPath]? 数组进行持续检查。

现在你有一个。当前选择 b.选择列表, 您可以继续根据需要将数据删除/添加到最终数组中。

因此,在您的情况下,在 func tableView(UITableView, didSelectRowAt: IndexPath) 中,您将检查 indexPathsForSelectedRows strong> 已经有节+行数。如果是这样,那么只需使用 tableView: UITableView, didHighlightRowAt indexPath: IndexPath) 中的突出显示,然后相应地更改存储所选电影列表的数组。如果不是简单地添加它。

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 2011-04-04
    • 2014-04-05
    • 2018-06-22
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2019-10-21
    相关资源
    最近更新 更多