我有一个表视图,其中有两个部分和单元格。用户可以从每个部分中只选择一个单元格,我已经尝试了一些代码进行选择,当我从第0部分选择任何单元格时,它会删除第1部分中的勾选符号,当我从第1部分中选择任何单元格时,它会从部分中删除勾选符号0和我也希望当表视图加载其每个部分的第一个单元格时应该预先选择。我在表格视图中有单一选择的代码,但是对于预选,我不知道如何预先选择单元格。这是我的代码,
extension FiltersVC: UITableViewDelegate,UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems[section].count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = filterTableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! FiltersTableViewCell
cell.tilteLbl.text = menuItems[indexPath.section][indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = #colorLiteral(red: 0.9130856497, green: 0.9221261017, blue: 0.9221261017, alpha: 1)
let headerText = UILabel()
headerText.textColor = UIColor.black
headerText.adjustsFontSizeToFitWidth = true
switch section{
case 0:
headerText.textAlignment = .center
headerText.text = "LIST BY"
headerText.backgroundColor = #colorLiteral(red: 0.9190355449, green: 0.9281349067, blue: 0.9281349067, alpha: 1)
headerText.font = UIFont.boldSystemFont(ofSize: 20)
case 1:
headerText.textAlignment = .center
headerText.text = "COUSINE"
headerText.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
headerText.font = UIFont.boldSystemFont(ofSize: 20)
default: break
}
return headerText
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if let cell = filterTableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
let item = menuItems[indexPath.section][indexPath.row]
UserDefaults.standard.set(item, forKey: "listBy")
UserDefaults.standard.synchronize()
filterBtn.isUserInteractionEnabled = true
filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
}
else {
if let cell = filterTableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
let item = menuItems[indexPath.section][indexPath.row]
UserDefaults.standard.set(item, forKey: "Cuisine")
UserDefaults.standard.synchronize()
filterBtn.isUserInteractionEnabled = true
filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
cell.accessoryType = .none
}
}
else{
if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
cell.accessoryType = .none
}
}
}
答案 0 :(得分:-1)
您需要启用多项选择。在viewDidLoad()
put:
tableView.allowsMultipleSelection = true
然后通过选中返回索引路径数组的didSelectRow()
,只处理tableView.indexPathsForSelectedRows
委托方法中每个部分中的一个选择。
例如:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Get list of all selected index paths (includes one just selected, so may be up to 3)
var selectedIndexPaths = tableView.indexPathsForSelectedRows!
// Remove index path that was just selected
if selectedIndexPaths.contains(indexPath) {
selectedIndexPaths.remove(at: selectedIndexPaths.index(of: indexPath)!)
}
if indexPath.section == 0 {
// Get any selected index paths in section 0
let selectionInSection0 = selectedIndexPaths.filter { $0.section == 0 }
if !selectionInSection0.isEmpty {
// Deselect the preciously selected row in section 0
tableView.deselectRow(at: selectionInSection0.first!, animated: true)
}
// Other required code from user
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
let item = menuItems[indexPath.section][indexPath.row]
UserDefaults.standard.set(item, forKey: "listBy")
UserDefaults.standard.synchronize()
filterBtn.isUserInteractionEnabled = true
filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
} else if indexPath.section == 1 {
// Get any selected index paths in section 1
let selectionInSection1 = selectedIndexPaths.filter { $0.section == 1 }
if !selectionInSection1.isEmpty {
// Deselect the preciously selected row in section 1
tableView.deselectRow(at: selectionInSection1.first!, animated: true)
}
// Other required code from user
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
let item = menuItems[indexPath.section][indexPath.row]
UserDefaults.standard.set(item, forKey: "Cuisine")
UserDefaults.standard.synchronize()
filterBtn.isUserInteractionEnabled = true
filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
}
}