swift - 在表格 View 单元格中使用按钮出列单元格

标签 swift uitableview

基本上,我有一个列出潜在员工的自定义表格 View 单元格。该单元格包含一些标签和一个按钮。我想要的是删除单元格的按钮,但我只能找到这个:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
  numbers.removeAtIndex(indexPath.row)    
  tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}

这只允许您在滑动然后选择“删除”时删除单元格

我想我应该在我的 tableViewController.swift 文件中创建一个函数来删除给定行的单元格,并在我的自定义单元格类中创建一个 IBAction 来调用该函数。

删除给定行的单元格的函数是什么样的?但是在这种情况下,单元格是否也知道它在哪一行或者这是 tableViewController 的工作?

最佳答案

  1. 在您的 cellForRowAtIndexPath 中设置 cell.button.tag = indexPath.row

  2. 给按钮添加一个目标:cell.button addTarget:...

  3. 在按钮方法中,删除 dataArray 中索引 button.tag 的项目。然后刷新tableView self.tableView reloadData

关于swift - 在表格 View 单元格中使用按钮出列单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36345149/

相关文章:

ios - 在具有编程约束的 UIView 中将 UILabel 和 UIImage 居中

ios - 加入 Firebase 数据库

objective-c - 更改 UITableViewCell 框架但没有用

ios - ImageView 没有回到合适的状态

ios - 如何截断 UITableView Cell TextLabel 中的文本,使其不隐藏 DetailTextLabel?

iphone - UITableView内容大小和行高

arrays - 基于另一个数组快速排序数组

swift - 类型 'string' 的值没有成员 'text'

ios - 日期格式化程序不从应用程序中的字符串返回日期,但可以在 Playground 上工作

ios - 自动增加/减少 UITableViewCell 中的 UILabelView 高度?