【发布时间】:2016-05-22 19:10:58
【问题描述】:
我在从 TableView 中选择任何行时遇到了这个奇怪的问题,另一行也被选中,比如我有一个 tableView 有多行,在我的情况下它是 11 并且启用了带有刻度标记附件的多选(当我选择一行,在所选行上标记了一个勾号)所以当我选择我的第一行时,第 8 行也被选中(我可以在第 8 行看到刻度线,但我只选择了第 1 行)当我选择另一个第 2 行时,我的第 9 行也被选中不知道为什么会这样:
var selectedTextLabels = [NSIndexPath: String]()
var selectedTextLabelsName = [NSIndexPath: String]()
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let cell = tableView.cellForRowAtIndexPath(indexPath) as! UsersTableViewCell
if (cell.accessoryType == UITableViewCellAccessoryType.Checkmark){
cell.accessoryType = UITableViewCellAccessoryType.None;
selectedTextLabels[indexPath] = nil
selectedTextLabelsName[indexPath] = nil
}else{
cell.accessoryType = UITableViewCellAccessoryType.Checkmark;
if (cell.accessoryType == UITableViewCellAccessoryType.Checkmark){
if let Id = cell.channelIDLbl?.text {
selectedTextLabels[indexPath] = Id
}
if let channelName = cell.lblChannelname?.text{
selectedTextLabelsName[indexPath] = channelName
}
}
}
cellForRowAtIndexpath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UsersTableViewCell
if isFavoritesTabActive == true {
let object : RChannels = self.favoritesArray.objectAtIndex(indexPath.row) as! RChannels
cell.lblChannelname.text = object.channelName
let favorite = object.isFavorite
if favorite == "true" {
cell.favIcon.image = UIImage(named: "Favourite")
return cell }
else {
let object : RChannels = self.noteObjects.objectAtIndex(indexPath.row) as! RChannels
cell.lblChannelname.text = object.channelName
let favorite = object.isFavorite
if favorite == "true" {
cell.favIcon.image = UIImage(named: "Favorite")
}else {
cell.favIcon.image = UIImage(named: "unFavorite") }
return cell
}
【问题讨论】:
-
你能分享
cellForRowAtIndexPath吗? -
您应该在 didSelectRowWithIndexPath 中更新您的模型,并在 cellForRowAtIndexPath 中设置附件类型。请记住 UITableViewCells 被重用。设置 accessoryType 并且在它出队后不重置它会导致它出现在它不打算出现的单元格中。
-
@ViktorSimkó 请检查我的问题
-
所以有什么线索可以在出队后重置它吗?你是对的,它只显示为选中,因为当我打印选定的行时,只有我实际选择的行在那里@beyowulf
-
标签: ios uitableview swift2