【发布时间】:2020-03-08 16:58:38
【问题描述】:
我想单击自定义 xib 文件中的按钮,然后它会转到使用可解码的 api 传递的链接。
我可以使用以下方法将整行重定向到来自 api 的链接:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let item = page!.tradeshows[indexPath.row]
if let eventURLS = item.url {
UIApplication.sharedApplication().openURL(eventURLS, options: [:], completionHandler: nil)
} else {print("link not working")
}
}
但我不想选择整行,我只想从自定义 .xib 文件中选择按钮。
我也试过了:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell",
for: indexPath) as!EventTableViewCell
let item = page!.tradeshows[indexPath.row]
cell.registerButton.addTarget(self, action: #selector(UpcomingEventsViewController.onClickedRegistrationButton(_: )),
for: .touchUpInside)
cell.registerButton.tag = indexPath.row
return cell
}
@objc func onClickedRegistrationButton(_ button: UIButton) {
let buttonLink = button.tag
}
但我不确定如何设置来自 json 数据的链接,以便使用第二种方法使 indexPath 正确。
【问题讨论】:
-
属性名以小写字母
tableData开始 -
最好将你的项目更新到最新的 swift ,swift 2 现在不能上传到商店
-
还有2个数组
page!.tradeshows和tableData决定哪1个是onClickedRegistrationButton内正确访问的表数据源 -
我更新了第一个代码 sn-p 所以它是我实际项目中的一个例子。在它只是相似之前。
标签: ios json swift api uitableview