RaviG RaviG - 5 months ago 32
iOS Question

After selecting a row another one row get selected automatically in UITableView

i am developing a application which has table view
in this table if i select on a row the checkmark appears
everything is working up to this

But the problem is when i selects a row one more row gets selected automatically
so please help me to solve this

i attached the code below
i tried it with both the options i.e. With Search Bar and without Search Bar
but problem is not solve
so please help

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}
}
}

Answer

I solved above problem by adding following code into cellForRowAtIndexPath method of UITableView

if tableView == self.searchDisplayController?.searchResultsTableView{
can = self.filteredCandy[indexPath.row]
}else{
can = self.candies[indexPath.row]
}