ios - 如何将 indexPath(不是 indexPath.row)传递到我的 UITableViewCell 的 IBAction

标签 ios uitableview swift ibaction

我正在开发一个待办事项应用程序。在我的应用程序中,我按下复选框按钮来删除一行。我编写这段代码是为了将 indexPath.row 传递到我的复选框按钮中:

cell.checkbox.tag = indexPath.row
cell.checkbox.addTarget(self, action: "checkAction:", forControlEvents: .TouchUpInside)

第一个代码允许我访问 indexPath.row,第二个代码允许我在按下按钮时创建一个函数。这是我在按下按钮时使用的功能:

@IBAction func checkAction(sender: UIButton) {
    taskMgr.removeTask(sender.tag)
    tblTasks.reloadData()
}

现在,我想在删除时添加动画,这样看起来就不会那么突然了。我将使用的代码是这样的:

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

但是,我只能在我的 checkAction 函数中访问 indexPath.row。如何访问 indexPath(不牺牲 indexPath.row)?

最佳答案

如果您在重新加载整个表格之前移动行或添加或删除行,标记可能会给您带来错误的行。因此,您可以在按钮方法中使用 indexPathForRowAtPoint: 来获取 indexPath,而不是使用标签。

@IBAction func checkAction(sender: UIButton) {

    let point = sender.convertPoint(CGPointZero, toView: self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(point)
    taskMgr.removeTask(indexPath.row)
    tblTasks.reloadData()
}

关于ios - 如何将 indexPath(不是 indexPath.row)传递到我的 UITableViewCell 的 IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431342/

相关文章:

iOS - 从 NSString 中获取数字的最佳方式! (地理坐标)

iphone - UITableView "Scroll down to update"

swift - UITableViewAutomaticDimension 的最小单元格高度

ios - 单击时无法更改 UItableViewCell 中按钮的图像

ios - 核心数据获取一对多关系数据

iphone - 即使在 iphone 中使用异步调用后下载图像也很慢

iphone - 用相同的方法满足特定条件后如何使计时器无效?

ios - UICollectionView 的框架未正确更新为 UITableViewCell subview

iphone - 如何使用单元格中的自定义按钮删除 UITableView 中的单元格?

ios - 随机唯一字符串生成用作随机数(oauth)