Ios 在UItableView中处理多个选定行
我正在尝试获取所有选定行的TextLabel,但我不知道如何进行搜索,但没有得到任何帮助,因此我自己尝试了一些方法,但效果不理想,我想创建一个数组并对选定单元格的TextLabel进行排序会很好(是的,效果很好)但是,当我取消选择一个单元格时,我想从数组中删除该单元格的文本标签,如果有人知道怎么做,请告诉我 这是我的密码-Ios 在UItableView中处理多个选定行,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试获取所有选定行的TextLabel,但我不知道如何进行搜索,但没有得到任何帮助,因此我自己尝试了一些方法,但效果不理想,我想创建一个数组并对选定单元格的TextLabel进行排序会很好(是的,效果很好)但是,当我取消选择一个单元格时,我想从数组中删除该单元格的文本标签,如果有人知道怎么做,请告诉我 这是我的密码- func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let cell = tableView.cellForRowAtIndexPath(indexPath)
if (cell?.accessoryType == UITableViewCellAccessoryType.Checkmark){
cell!.accessoryType = UITableViewCellAccessoryType.None;
getTheChannelNames.removeAtIndex(getTheChannelNames.count - 1)
print(getTheChannelNames.count)
print(getTheChannelNames)
}else{
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark;
_ = self.ChannelList[indexPath.row]
if (cell?.accessoryType == UITableViewCellAccessoryType.Checkmark){
getTheChannelNames.append((cell?.textLabel?.text)!)
print(getTheChannelNames.count)
print(getTheChannelNames)
}
}
}
有几种方法可以处理这个问题。首先,跟踪所选文本标签的indexPath对您来说非常重要。这样做可以避免排序。字典更适合做这项工作
var selectedTextLabels = [NSIndexPath: String]()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = .Checkmark
if let text = cell.textLabel?.text {
selectedTextLabels[indexPath] = text
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = .None
selectedTextLabels[indexPath] = nil
}
如果选择了单元格,文本标签将添加到字典中。相反,当它被取消选择时,它将从字典中删除
第二种解决方案是返回所有选定文本标签的方法:
func selectedTextLabels() -> [String]? {
return self.tableView.indexPathsForSelectedRows?.flatMap {
let cell = tableView.cellForRowAtIndexPath($0)!
return cell.textLabel?.text
}
}
它获取所有选定的索引路径,并获取每个路径的文本标签 与其存储文本,不如简单地存储一组选定的行,即可以从数据模型轻松获取的文本。您可以使用
NSMutableIndexSet
来存储选择状态好的,那么nsmutablewindexset
就是获取多个值所需的吗?感谢您的回答:)这是一个用于存储标记(行)的良好数据结构,您可以快速检查给定索引是否在集合中;在didSelectRowAtIndexPath
中,您可以检查该行是否已在集合中;如果不是,则添加它,如果是,则删除它。在cellforrowatinexpath
中,您可以检查集合,查看是否应添加或删除复选标记