UItableview中的UI按钮,每个单元格中的每个按钮具有不同的操作

时间:2016-07-28 11:07:22

标签: swift uitableview uibutton

在我的UITableView我试图为每个单元格设置一个不同的操作按钮,即链接到不同的viewController,对于每个按钮,但我只是设法实现它之一。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return courseTitle.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

    cell.image_view.image = courseImages[indexPath.row]
    cell.title.text = courseTitle[indexPath.row]
    cell.tableButton.tag = indexPath.row
    cell.tableButton.addTarget(self, action: "Test", forControlEvents: UIControlEvents.TouchUpInside)

     return cell
}

@IBAction func Test(sender: AnyObject){

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

}

2 个答案:

答案 0 :(得分:2)

如果你只有很少的细胞,你可以这样做:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

    cell.image_view.image = courseImages[indexPath.row]
    cell.title.text = courseTitle[indexPath.row]
    cell.tableButton.tag = indexPath.row

    switch indexPath.row {

    case 0:
        cell.tableButton.addTarget(self, action: "TestA", forControlEvents: UIControlEvents.TouchUpInside)

    case 1:
        cell.tableButton.addTarget(self, action: "TestB", forControlEvents: UIControlEvents.TouchUpInside)

    case 2:
        cell.tableButton.addTarget(self, action: "TestC", forControlEvents: UIControlEvents.TouchUpInside)

    default:
        cell.tableButton.addTarget(self, action: "TestD", forControlEvents: UIControlEvents.TouchUpInside)
    }

    return cell
}

func TestA(sender: AnyObject){   // no need for @IBAction as you set the target action while setting the cell
    self.performSegueWithIdentifier("SuccessfulLogin", sender: nil)
}

func TestB(sender: AnyObject){
    print("Second button action")
}

func TestC(sender: AnyObject){
    print("Third button action")
}

func TestD(sender: AnyObject){
    print("All other buttons action")
}

答案 1 :(得分:1)

您可以使用该标记值并更改selector语法

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

    cell.image_view.image = courseImages[indexPath.row]
    cell.title.text = courseTitle[indexPath.row]
    cell.tableButton.tag = indexPath.row
    cell.tableButton.addTarget(self, action: "test:", forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}

@IBAction func test(sender: UIButton){

    let row = sender.tag
    switch(row) {
        case 0:
            self.performSegueWithIdentifier("Segue1", sender: nil)
        case 1:
            self.performSegueWithIdentifier("Segue2", sender: nil)
        case 2:
            self.performSegueWithIdentifier("Segue3", sender: nil)
        default:
    }
}