Reputation: 450
I'm trying to implement a "Done" method through a custom slide left on an item in a table view. But when I click the "done" button it doesn't update my custom cell accessory type. (Found in the "editActionsForRowAtIndexPath" function (or search for "//mark as completed")") I've literally tried everything I could think of for many hours and I'm just stuck. Please note I'm very new to this, so if you could help me out in understandable words that would be great.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as CustomTableViewCell
// Configure the cell...
let bucketLists = (searchController.active) ? searchResults[indexPath.row] : bucketList[indexPath.row]
cell.nameLabel.text = bucketLists.name
cell.thumbnailImageView.image = UIImage(data: bucketLists.image)
cell.locationLabel.text = bucketLists.location
cell.typeLabel.text = bucketLists.type
if (bucketLists.isVisited == true) {
cell.favorIconImageView.hidden = false // Don't hide custom cell accessory if true
} else if (bucketLists.isVisited == false) {
cell.favorIconImageView.hidden = true
}
// Circular image
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width / 2
cell.thumbnailImageView.clipsToBounds = true
return cell
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
// Delete Button
var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete",handler: {
(action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
// Delete the row from the data source
if let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext {
let bucketListToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as BucketList
managedObjectContext.deleteObject(bucketListToDelete)
var e: NSError?
if managedObjectContext.save(&e) != true {
println("delete error: \(e!.localizedDescription)")
}
}
})
// Mark as Completed Button
var completedAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done",handler: {
(action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
println("Completed item \(indexPath.row)")
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as CustomTableViewCell
// Configure the cell...
let bucketLists = (self.searchController.active) ? self.searchResults[indexPath.row] : self.bucketList[indexPath.row]
cell.favorIconImageView.hidden = false // Item completed so hide the cell accessory image
})
deleteAction.backgroundColor = UIColor(red: 244.0/255.0, green: 67.0/255.0, blue: 54.0/255.0, alpha: 1.0) // Red
completedAction.backgroundColor = UIColor(red: 3.0/255.0, green: 169.0/255.0, blue: 244.0/255.0, alpha: 1.0) // Blue
return [deleteAction, completedAction]
}
If you need any more info please comment below
Upvotes: 3
Views: 269
Reputation: 104082
I think your problem is this line in the block for the definition of the var, completedAction
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as CustomTableViewCell
This instantiates a new cell that never appears in your table view. You want to get a reference to the cell you already have at that indexPath,
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as CustomTableViewCell
Upvotes: 1