Reputation: 6484
How can I disable interaction with all cells
except the selected one, and then enable the interaction after cell is selected again?
I tried like this inside didSelectRowAtIndexPath
method:
if cell.isExpanded
{
UIView.animateWithDuration(0.3) {
cell.contentView.layoutIfNeeded()
self.tableView.userInteractionEnabled = false
cell.userInteractionEnabled = true
}
}
But this will disable the entire tableView
including the currently selected cell
.
Upvotes: 0
Views: 440
Reputation: 72410
As of first your tableView
need to select cell, after that one of the cell is selected you want other cell not to select except that selected cell, for that you can create one instance property of type NSIndexPath
and use this to store inside didSelectRowAtIndexPath
, and compare its value inside cellForRowAtIndexPath
like this way.
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()
}
Note: I have set the cell userInteractionEnabled
to true again if you select your expanded cell.
Upvotes: 3
Reputation: 711
Use
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
to findout the selected cell and store the indexPath selected in a variable and reload the tableview.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
disable all the cells except the one selected by checking the stored indexPath.
Hope this helps .
Upvotes: 0