2017-04-14 17 views
0

我在uitableview中遇到问题,只要我选择一个单元格4没有单元格自动获取选择...我厌倦了这个问题有人请帮助这里是我的didselectrowfunc ..UItableview一次点击多个单元格正在检查迅速3

FUNC的tableView(_的tableView:UITableView的,didSelectRowAt indexPath:indexPath){

////////////////添加复选框///////// ///////////////

if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark 
{ 
if selectedDisease.count > 0 
{ 

let no = selectedDisease.index(of: finall[indexPath.row]) 
selectedDisease.remove(at: no!) 
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none 
print(selectedDisease) 
} 
else 
{ 
selectedDisease = [""] 
} 

} 
else 
{ 

if selectedDisease.contains("") 
{ 
selectedDisease.removeAll() 
var name = finall[indexPath.row] 
selectedDisease.append(name) 
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark 
print(selectedDisease) 

} 
else 
{ 
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark 
selectedDisease.append(finall[indexPath.row]) 
//selectedDisease = [finall[indexPath.row]] 
print(selectedDisease) 
} 

} 

} 

///////// CellForRowAt ///////////////

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "diseasetable", for: indexPath) as! FoodTableViewCell 

     cell.DiseaseName.text = finall[indexPath.row] 
     //indexsave.append(indexPath.row) 

     return cell 
    } 
+0

这是因为表格单元格是reusable.What你想干什么? –

+0

我想让用户选择多种疾病,但问题是如何做到这一点,通过这种代码自动选择不同的细胞..... **我知道它是重用细胞,但如何克服这一点,当表重用细胞它是未检查** –

+0

添加cellForRow方法的代码 –

回答

2

试试这个逻辑希望这将解决您的问题

let dict = [NSIndexPath: String]() 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

     if let _ = // dict contains value at indexpath { 
      cell.accessoryType = .Checkmark 
     } else { 
      cell.accessoryType = .None 
     } 

     return cell 
    } 


    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      if (// dict contains value == indexpath) { 
       // remove value from dict 
      } else { 
      //add value to dict 
      } 
      tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
     } 
+0

谢谢很多人@Anil Kumar –

+0

欢迎@HammerClass –

-1

只要利用prepareForReuse方法UITableViewCell。在你FoodTableViewCell类实现prepareForReuse menthod像下面

class FoodTableViewCell: UITableViewCell 
{ 
    override func awakeFromNib() 
    { 
     super.awakeFromNib() 
     // Initialization code 
    } 
    override func setSelected(selected: Bool, animated: Bool) 
    { 
     super.setSelected(selected, animated: animated) 
     // Configure the view for the selected state 
    } 
    override func prepareForReuse() 
    { 
     self.accessoryType = UITableViewCellAccessoryType.none 
     //add code to reset selection if there is any other selection mechanism 
    } 
} 
+0

通过使用这种方法,当我向下滚动并回滚选定的单元格时会自动取消选中... @raki –

+0

添加选择逻辑内部cellForRowAtIndexPath方法,这将解决您的问题 – raki