UITableView-在不同部分中选择多个和单个 [英] UITableView - Multiple and Single selection in different sections
问题描述
我有一个带有两个不同部分的 UITableView
.是否可以在一个部分中进行多项选择,而在另一部分中进行一项选择呢?如果我使用:
I have a UITableView
with 2 different sections. Is it posible to have multiple selection in one section and single selection in the other? If I use:
self.tableView.allowsMultipleSelection = YES;
它会影响整个表.
推荐答案
我将展示我自己的解决方案,该解决方案与您发布的解决方案几乎相同.我认为这要好一些,因为我手动管理选定的行,因此以后可以使用 tableView.indexPathsForSelectedRows
属性.
I'll expose my own solution, that is pretty much the same one as the one you posted. I think it's a tad better though, since I manually manage the selected rows so I can use the tableView.indexPathsForSelectedRows
property later on.
它是用Swift 2编写的.
It's written in Swift 2.
我首先声明一个NSIndexPath,它将存储先前选择的项目
I first declare a NSIndexPath that will store the previously selected item
var indexPathOfPreviouslySelectedRow: NSIndexPath?
然后我使用这些tableView委托函数(我的单选部分为0,我的多选部分为1)
I then use these tableView delegate functions (my single selection section is 0, my multiple selection is 1)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.section {
case 0:
if let previousIndexPath = indexPathOfSelectedRowPaidBy {
tableView.deselectRowAtIndexPath(previousIndexPath, animated: false)
tableView.cellForRowAtIndexPath(previousIndexPath)?.accessoryType = UITableViewCellAccessoryType.None
}
indexPathOfSelectedRowPaidBy = indexPath
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
case 1:
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
default:
break
}
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.section {
case 0:
if let previousIndexPath = indexPathOfSelectedRowPaidBy {
tableView.deselectRowAtIndexPath(previousIndexPath, animated: false)
tableView.cellForRowAtIndexPath(previousIndexPath)?.accessoryType = UITableViewCellAccessoryType.None
}
indexPathOfSelectedRowPaidBy = nil
case 1:
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.None
default:
break
}
}
这篇关于UITableView-在不同部分中选择多个和单个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!