ios - Tableview 不重新加载自定义附件类型

标签 ios uitableview swift

我正在尝试通过表格 View 中项目上的自定义幻灯片来实现“完成”方法。 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]
}

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

最佳答案

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

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

这会实例化一个从未出现在您的 TableView 中的新单元格。您想获得对您在该 indexPath 中已有的单元格的引用,

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

关于ios - Tableview 不重新加载自定义附件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28648081/

相关文章:

ios - ios 模拟器可以在 Mac OS X Server 上运行吗?

ios - 'UITableViewCell ?' does not have a member named ' 文本标签'

ios - 将 subview 添加到 UITableViewController

ios - UITableView 滚动跳转

iphone - 解析xml内容标签

iphone - 加载 UIView 到 navigationalStack 不传递字符串到新 View

swift - 在 Swift 2 中使用 Mapkit 从用户位置路由到点注释

ios - 带参数的链式序列可观察对象

swift - 导航到另一个 xib 时未在 AppKit(主)线程问题上运行

iphone - 如何在方向更改后调整 UIView 的大小(包含的代码似乎不起作用)