gpt4 book ai didi

ios - 选择一行后,UITableView 会自动选择另一行

转载 作者:行者123 更新时间:2023-11-30 13:47:42 26 4
gpt4 key购买 nike

我正在开发一个具有表格 View 的应用程序,每个 TableView 单元格仅包含标签,我已将电池配件类型设置为 CheckMark,如果我选择一行,则会出现复选标记一切都在进行中

但是当我选择一行并向下滚动表格 View 时,其他行上会出现复选标记,所以请帮助我

我已附上以下代码

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let selectRow = self.MytableView.cellForRowAtIndexPath(indexPath)!
var can: Candy
if tableView == self.searchDisplayController?.searchResultsTableView{
can = self.filteredCandy[indexPath.row]
}else{
can = self.candies[indexPath.row]
}
let name = "\(can.firstName) \(can.lastName)"
if selectRow.accessoryType == UITableViewCellAccessoryType.None{
selectRow.accessoryType = UITableViewCellAccessoryType.Checkmark
selectRow.tintColor = UIColor.blackColor()
DataStorageManager.sharedInstance.guests.append(name)
}else{
selectRow.accessoryType = UITableViewCellAccessoryType.None
DataStorageManager.sharedInstance.guests == DataStorageManager.sharedInstance.guests.filter(){$0 != name}
}
}

最佳答案

您犯了逻辑错误,即您要对新单元格进行复选标记,但没有对最后选定的行进行取消复选标记。所以请执行我的逻辑。首先创建一个全局变量,例如

 let selectedRow = 0

And then implement this one

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedIndexPath = NSIndexPath.init(forItem: selectedRow, inSection: 0)
let selectedCell = tableView.cellForRowAtIndexPath(selectedIndexPath)! as UITableViewCell

selectedCell.accessoryType = .None

let selectRow = self.MytableView.cellForRowAtIndexPath(indexPath)!
var can: Candy
if tableView == self.searchDisplayController?.searchResultsTableView{
can = self.filteredCandy[indexPath.row]
}else{
can = self.candies[indexPath.row]
}
let name = "\(can.firstName) \(can.lastName)"
if selectRow.accessoryType == UITableViewCellAccessoryType.None{
selectRow.accessoryType = UITableViewCellAccessoryType.Checkmark
selectRow.tintColor = UIColor.blackColor()
DataStorageManager.sharedInstance.guests.append(name)
}else{
selectRow.accessoryType = UITableViewCellAccessoryType.None
DataStorageManager.sharedInstance.guests == DataStorageManager.sharedInstance.guests.filter(){$0 != name}
}

selectedRow = indexPath.row
}

关于ios - 选择一行后,UITableView 会自动选择另一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34717262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com