我正在尝试通过表格 View 中项目上的自定义幻灯片来实现“完成”方法。 但是当我点击“完成”按钮时,它不会更新我的自定义电池附件类型。 (在“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/