无法将操作连接到静态UITableViewCell?

时间:2015-06-12 11:24:50

标签: ios swift uitableview

我无法将操作链接到Interface Builder中设计的静态UITableViewCell。我有TableViewController(作为UITableViewController),它是tableView的数据源和代理。我试图连接的方法如下:

@IBAction func openWebsite(AnyObject) {
        UIApplication.sharedApplication().openURL(NSURL(string: "http://google.co.uk")!)
}

我尝试从Inspector中拖动操作,但它不会将自身应用于UITableViewCell。我也做了相反的事情并尝试将一个连接从Interface Builder拖到TableViewController,在那里它只作为插座而不是动作连接?

3 个答案:

答案 0 :(得分:1)

即使对于静态单元格,我建议您添加委托方法didSelectRowAtIndexPath,然后响应该操作,因为设置此类单元格的操作可能会很棘手。 UITableView单元有许多底层视图,并且对其中一些单元添加操作可能会有问题。

答案 1 :(得分:0)

可以做两件事:

  1. 将方法声明更改为以下内容并拖动:

    @IBAction func openWebsite(sender:AnyObject){     UIApplication.sharedApplication()。openURL(NSURL(字符串:" http://google.co.uk")!)//缺少参数名称 }

  2. 将动作拖放到控制器时,请从弹出菜单中将IBOutlet更改为IBAction。

答案 2 :(得分:0)

使用单元格的索引,然后从中调用您的函数。

override func tableView(tableView: UITableView, 
    didSelectRowAtIndexPath indexPath: NSIndexPath) {

    print(indexPath.section)
    print(indexPath.row)

    if indexPath.section == 1 && indexPath.row == 1 {
        // call function here
        openWebsite()
    }

}
相关问题