我有一个UITableView,每个单元格都与一个文件关联。我正在使用一个自定义的tableview单元格来显示文件标题,并且每个单元格都具有一个编辑UIButton,该按钮应该可以连接到另一个允许用户编辑文件标题的视图控制器。我的问题是,当我按下编辑按钮时,由于没有调用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/

10-12 00:23
查看更多