gpt4 book ai didi

ios - Swift UITableview Custom Cell with multiple section array and row data array Search 实现

转载 作者:行者123 更新时间:2023-11-28 07:23:27 25 4
gpt4 key购买 nike

在我的场景中,我正在尝试创建具有多个部分和单元格的 UITableview 自定义单元格。在这里,我想添加两个复选标记和 search 选项,我尝试使用下面的代码但两者都不起作用。

在我的代码下面。如何解决这个问题?

class  CustomCell: UITableViewCell {

@IBOutlet weak var NameLabel: UILabel!
@IBOutlet weak var CountLabel: UILabel!

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
}
}


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

var searchController : UISearchController!
let sectionData = ["1", "2"]
let rowNames = [["A", "B", "C"], ["D", "E", "F"]]
var filteredData: [[String]]!
let cellReuseIdentifier = "cell"
@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()
filteredData = rowNames
}


//MARK: List Tableview
func numberOfSections(in tableView: UITableView) -> Int {
return sectionData.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionData[section]
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData[section].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CustomCell
cell.NameLabel.text = filteredData[indexPath.section][indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
}

//MARK: Search Delegate

func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
filteredData = searchText.isEmpty ? rowNames : rowNames.filter({(dataString: String) -> Bool in
return dataString.range(of: searchText, options: .caseInsensitive) != nil
})
Self.tableView.reloadData()
}
}
}

最佳答案

我会推荐一些像这样的改变

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

var searchController : UISearchController!
var rowNames = [[(value: "A",selected: false),(value: "B",selected: false),(value:"C",selected:false)],[(value: "D",selected: false),(value: "E",selected:false),(value:"F",selected:false)]]
var filteredData: [[(value:String,selected:Bool)]]!
let cellReuseIdentifier = "cell"
@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()
//filteredData = rowNames
}


//MARK: List Tableview
func numberOfSections(in tableView: UITableView) -> Int {
return rowNames.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return (section + 1).description
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.searchBar.text != nil && searchController.searchBar.text != ""{
return filteredData.count
}else{
return rowNames[section].count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CustomCell
if searchController.searchBar.text != nil && searchController.searchBar.text != ""{
cell.NameLabel.text = filteredData[indexPath.section][indexPath.row].value
}else{
cell.NameLabel.text = rowNames[indexPath.section][indexPath.row].value
}

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if searchController.searchBar.text == nil || searchController.searchBar.text == ""{
if rowNames[indexPath.section][indexPath.row].selected == true{
cell?.accessoryType = .none
rowNames[indexPath.section][indexPath.row].selected = false
}else{
cell?.accessoryType = .checkmark
rowNames[indexPath.section][indexPath.row].selected = true
}
}

//self.tableView.deselectRow(at: indexPath, animated: true)
}

//MARK: Search Delegate

func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text, searchController.searchBar.text != "" {
})
filteredData = rowNames.map({row in
row.filter({
$0.value.contains(searchText)
}).map({$0})
})
self.tableView.reloadData()
}
}

}由于您在所选单元格上设置复选标记,因此您必须在数据源中跟踪它的状态。所以这就是为什么如果你使用一个结构,模型会变成一个元组,你可以在你的

didSelectRow 函数,您必须跟踪模型的状态以显示或不显示复选标记。搜索不值得搜索,因为没有搜索文本,因此应用不应重新加载 tableView。

关于ios - Swift UITableview Custom Cell with multiple section array and row data array Search 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com