我的情况是,我尝试使用UITableview
和custom cell
创建sections
。在这里,我也尝试一次checkmark
进行one cell
选择。如果用户单击该部分,则该部分下的所有相关行都应像组选择一样显示复选标记。它无法正常工作。另外,在这种情况下搜索主要复合体之一。提供一些解决办法。
下面我正在使用的代码,
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet weak var myCellLabel: UILabel!
override func setSelected(_ selected: Bool, animated: Bool) { // Check Mark Selection Not Properly Working
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
}
}
class ViewController: UIViewController,UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {
// These strings will be the data for the table view cells
let sectionNames = ["pizza", "deep dish pizza", "calzone"]
let data = [["Margarita", "BBQ Chicken", "Pepperoni"],
["sausage", "meat lovers", "veggie lovers"],
["sausage", "chicken pesto", "prawns", "mushrooms"]]
//var filteredData: [[String]]!
let cellReuseIdentifier = "cell"
var searchController : UISearchController!
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//filteredData = data
self.tableView.rowHeight = 78.5
}
@IBAction func searchAction(_ sender: Any) {
// Create the search controller and specify that it should present its results in this same view
searchController = UISearchController(searchResultsController: nil)
// Set any properties (in this case, don't hide the nav bar and don't show the emoji keyboard option)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.keyboardType = UIKeyboardType.asciiCapable
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.barTintColor = #colorLiteral(red: 0.3118804693, green: 0.435135603, blue: 0.9917090535, alpha: 1)
searchController.searchBar.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
// Make this class the delegate and present the search
self.searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
present(searchController, animated: true, completion: nil)
}
// MARk: - Tableview Delegates
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.myCellLabel.text = data[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionNames[section]
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedValue = data[indexPath.section][indexPath.row]
//send selectedValue to server...
}
func updateSearchResults(for searchController: UISearchController) { // SEARCH NOT WORKING
if let searchText = searchController.searchBar.text {
filteredData = searchText.isEmpty ? data : data.filter({(dataString: String) -> Bool in
return dataString.range(of: searchText, options: .caseInsensitive) != nil
})
tableView.reloadData()
}
}
}
答案 0 :(得分:0)
尝试一下-
将此行添加到其注释掉的地方。
var filteredData: [[String]] = data
然后将您的UITableViewDatasource方法更改为使用filteredData
而不是data
。例如
func numberOfSections(in tableView: UITableView) -> Int {
return filteredData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.myCellLabel.text = filteredData[indexPath.section][indexPath.row]
return cell
}
data
仅应在搜索委托函数中使用。
答案 1 :(得分:0)
我建议您在模型中采用这些结构
file cells1.tif
这些结构将在单元格中同时包含节名称和选择。在您的班级中,您的模型就像这样:
struct MyData {
var value: String?
var selected: Bool = false
}
struct tableData{
var sectionName: String
var data: [MyData]
}
由于您正在重复使用同一表视图进行搜索,因此必须检查使用了哪些数据源,如果搜索栏处于活动状态但尚无输入,则不要重新加载数据
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
var rowNames = [tableData]()
var filteredData: [tableData]!
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 rowNames[section].sectionName
}
PD:这里缺少一件事,当在标题上敲击时如何选择所有行,但是这是您应该提出的另一个问题。