2016-08-23 61 views
0

如何禁用除所选单元之外的所有cells交互,然后在再次选择单元之后启用交互?如何禁用除所选单元之外的所有单元

我试图像这里面didSelectRowAtIndexPath方法:

if cell.isExpanded 
    { 
     UIView.animateWithDuration(0.3) { 
      cell.contentView.layoutIfNeeded() 
      self.tableView.userInteractionEnabled = false 
      cell.userInteractionEnabled = true 
     } 
    } 

但是,这将禁用整个tableView包括当前选定的cell

+0

您是否阅读过表格视图委托方法? – Desdenova

+0

你建议使用哪一个来完成它? – DCDC

+0

禁用单元格选择的那个。 – Desdenova

回答

0

使用

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 

到findout所选择的小区和存储在一个变量中选择的indexPath和重新加载的tableview。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

禁用除该一个通过检查存储indexPath选择所有单元格。

希望这会有所帮助。

3

作为第一你tableView需要选择单元格,单元格中的一个被选中后,你想其他细胞不同的是所选单元格不选择,对于您可以创建NSIndexPath类的一个实例属性,并使用它来存储在didSelectRowAtIndexPath之内,并且像这样比较它在cellForRowAtIndexPath里面的值。

var selectedIndexPath: NSIndexPath? 

的cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell 
    if (self.selectedIndexpath != nil) { 
     if self.selectedIndexpath == indexPath { 
      cell.userInteractionEnabled = true 
     } 
     else { 
      cell.userInteractionEnabled = false 
     } 
    } 
    else { 
     cell.userInteractionEnabled = true 
    } 
    return cell 
} 

didSelectRowAtIndexPath方法

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if self.selectedIndexpath != indexPath { 
     self.selectedIndexpath = indexPath 
    } 
    else { 
     self.selectedIndexpath = nil 
    } 
    tableView.reloadData() 
} 

注:我有,如果你选择增殖的细胞的细胞userInteractionEnabled再次设置为true。

+0

试过了,这不适用于我的情况,因为这个早期的数据重新加载搞砸了我的单元格 – DCDC

+0

没有重新加载tableView的东西应该在我看来 – DCDC

+0

@DCDC我不这么认为没有重新加载它是可能的,因为你希望其他单元格变为禁用。 –

相关问题