如何在 swift 中一次只能选择两个 UITableview 单元格
How to make only two cells of UITableview selectable at a time in swift
是否可以一次只选择 UITableview 的两个单元格?目前我只能设置UITableView的单选或多选。
请问有人可以post Swift3 中的想法或代码吗?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier") as! UITableViewCell
let currentItem = data[indexPath.row]
if currentItem.selected {
cell.imageView!.image = UIImage(named:"check")!
cell.textLabel!.font = UIFont(name:"OpenSans-Bold", size:15)
} else {
cell.imageView!.image = nil
cell.textLabel!.font = UIFont(name:"OpenSans-Regular", size:15)
}
return cell
}
当单元格被 select 编辑时,您将在 didSelectRowAtIndex
中收到回调。因此,您可以跟踪 selected 单元格并相应地删除 select 单元格。使用数组跟踪所有 selected 单元格
var selectedIndexes = [Int]()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (selectedIndexes.contains(indexPath.row)) {
selectedIndexes.remove(at: selectedIndexes.index(of: indexPath.row)!)
} else {
if selectedIndexes.count == 2 {
selectedIndexes[0] = indexPath.row
} else {
selectedIndexes.append(indexPath.row)
}
}
tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier") as! UITableViewCell
let currentItem = data[indexPath.row]
if selectedIndexes.contains(indexPath.row) {
cell.imageView!.image = UIImage(named:"check")!
cell.textLabel!.font = UIFont(name:"OpenSans-Bold", size:15)
} else {
cell.imageView!.image = nil
cell.textLabel!.font = UIFont(name:"OpenSans-Regular", size:15)
}
return cell
}
是否可以一次只选择 UITableview 的两个单元格?目前我只能设置UITableView的单选或多选。
请问有人可以post Swift3 中的想法或代码吗?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier") as! UITableViewCell
let currentItem = data[indexPath.row]
if currentItem.selected {
cell.imageView!.image = UIImage(named:"check")!
cell.textLabel!.font = UIFont(name:"OpenSans-Bold", size:15)
} else {
cell.imageView!.image = nil
cell.textLabel!.font = UIFont(name:"OpenSans-Regular", size:15)
}
return cell
}
当单元格被 select 编辑时,您将在 didSelectRowAtIndex
中收到回调。因此,您可以跟踪 selected 单元格并相应地删除 select 单元格。使用数组跟踪所有 selected 单元格
var selectedIndexes = [Int]()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (selectedIndexes.contains(indexPath.row)) {
selectedIndexes.remove(at: selectedIndexes.index(of: indexPath.row)!)
} else {
if selectedIndexes.count == 2 {
selectedIndexes[0] = indexPath.row
} else {
selectedIndexes.append(indexPath.row)
}
}
tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier") as! UITableViewCell
let currentItem = data[indexPath.row]
if selectedIndexes.contains(indexPath.row) {
cell.imageView!.image = UIImage(named:"check")!
cell.textLabel!.font = UIFont(name:"OpenSans-Bold", size:15)
} else {
cell.imageView!.image = nil
cell.textLabel!.font = UIFont(name:"OpenSans-Regular", size:15)
}
return cell
}