ios - 不调用didSelectRowAtIndexPath获取UITableViewCell数据

标签 ios swift uitableview

我有一个UITableView,每个单元格都与一个文件关联。我使用自定义表格 View 单元格来显示文件标题,每个单元格都有一个编辑 UIButton,该按钮应该连接到另一个允许用户编辑文件标题的 View Controller 。我的问题是,当我点击编辑按钮时,我不确定如何将与单元格关联的文件数据发送到下一个 View Controller ,因为 didSelectRowAtIndexPath 没有被调用。

struct Files {
    var title: String?
    var username: String?
    var url: String?
    var fileId: Int?
    var userId: Int?
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.fileTable.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FileCell

    let file = self.tableFiles[indexPath.row]
    cell.fileLabel.text = file.title
    cell.userLabel.text = file.username

    cell.editButton.addTarget(self, action:#selector(HomeController.editPressed), forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}

func editPressed() {

    self.performSegueWithIdentifier("HomeToEdit", sender: nil)

}

最佳答案

将 editPressed 更改为:

func editPressed(sender: UIButton!) {...}

当选择器添加为目标时,您需要将 : 添加到选择器

您可以通过调用sender.superView找到按钮的父单元格。将其转换为 FileCell,然后您可以访问它的 fileLabel.text(如果足够的话)。如果没有,您可以通过调用 indexPathForCell 找到单元格的索引。

关于ios - 不调用didSelectRowAtIndexPath获取UITableViewCell数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820795/

相关文章:

ios - 为 UIImageView 禁用大型 UIImage

ios - 当我添加 self.init(url : url)?

ios - 单击提交按钮后从 ortuman/Swift Forms 检索数据

ios - 小 View 在多个 View 上实时显示数据

ios - 以编程方式快速将搜索栏添加到 TableView

ios - 以编程方式更改 UITableViewStyle 和单元格 - 在 Storyboard中制作,在代码中修改

macos - 如何制作一个 MenuBar 应用程序(使用 Swift),它不仅包含菜单项列表?

iphone - UITableView 滚动是否以一定的速度平滑?

ios - UItableview从下往上重新加载动画

ios - 推送时 UISearchController 不隐藏 View