我是 ios swift4 的新手,我只是在 TableView Controller 中创建选中和取消选中按钮,我的任务是使用选中按钮选择表中的项目,我想取消选中单击取消选中按钮时选中的项目。帮助我来解决这个问题
最佳答案
在 daysRemainderTableView
中使用名为 daysSelectionimageView
的 imageview。这是在 tableviewcell 中选择和取消选择行的示例。
@IBOutlet weak var daysRemainderTableView: UITableView!
var days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
var selectedDaysArray = [Int]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return days.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RemainderDaysCell", for: indexPath)as! RemainderDaysCell
cell.remainderDaysLabel.text = "\(days[indexPath.row])"
if selectedDaysArray.contains(indexPath.row){
cell.daysSelectionimageView.image = #imageLiteral(resourceName: "selectedImage")
}else{
cell.daysSelectionimageView.image = #imageLiteral(resourceName: "ring")
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? RemainderDaysCell
{
if selectedDaysArray.isEmpty{
self.selectedDaysArray = [Int]()
}
if selectedDaysArray.contains(indexPath.row){
if let indexElement = selectedDaysArray.index(of: indexPath.row){
cell.daysSelectionimageView.image = #imageLiteral(resourceName: "ring")
selectedDaysArray.remove(at: indexElement)
}
}else{
cell.daysSelectionimageView.image = #imageLiteral(resourceName: "selectedImage")
selectedDaysArray.append(indexPath.row)
}
}
}
我正在indexpath和didSelectRow ai indexpath处的cellforrow中检查selectedDaysArray
,如果selectedDaysArray已选择索引,则我将删除元素,如果不是,则将indexemement附加到selectedDaysArray。
关于ios - 选中和取消选中 TableView Controller 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533637/