Tableview没有重新加载自定义配件类型

时间:2015-02-21 16:26:19

标签: ios uitableview swift

我试图实施一个"完成"方法通过自定义幻灯片留在表视图中的项目上。 enter image description here 但是当我点击"完成"按钮它不会更新我的自定义单元配件类型。 (找到" editActionsForRowAtIndexPath"功能(或搜索" //标记为已完成")")我真的尝试了一切我想了好几个小时,我才被卡住了。请注意我对此非常陌生,所以如果你能用可以理解的话来帮助我,那就太好了。

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

如果您需要更多信息,请在下面发表评论

1 个答案:

答案 0 :(得分:1)

我认为你的问题是块中的这一行,用于定义var,completedAction

let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as CustomTableViewCell

这会实例化一个从未出现在表视图中的新单元格。您希望获得对该indexPath上已有的单元格的引用,

让cell = self.tableView.cellForRowAtIndexPath(indexPath)为CustomTableViewCell