我有一个带有自定义单元格的PFQueryTableViewController,每个单元格都有1个标签和1个按钮,标签(Place)是一个文本,而按钮(Tickets),当你单击它时你会转到一个url。我已经将标签和网址的文本存储为Parse.com上的字符串,但我的问题是如何解决这个问题,让每个按钮都能动作并从我的解析数据库中打开这个存储的URL。
这是代码
class Shows: PFQueryTableViewController {
override func queryForTable() -> PFQuery {
let query = PFQuery(className: "Shows")
query.orderByDescending("createdAt")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ShowsCell
let showplace = object?.objectForKey("Place") as? String
let showtickets = object?.objectForKey("Tickets") as? String
cell.place.text = showplace
cell.tickets.tag = indexPath.row
cell.tickets.addTarget(self, action: #selector(ticketsaction), forControlEvents: .TouchUpInside)
return cell
}
}
这是Button Action
@IBAction func ticketsaction(sender: UIButton){
let ticketsurl = String()
let url = NSURL(string: "\(ticketsurl)")
UIApplication.sharedApplication().openURL(url!)
}
答案 0 :(得分:0)
您可以使用其单元格的indexPath.row
标记每个按钮,并从您用于填充列表的阵列中获取特定的URL。只需将button.tag
等同于indexPath.row
。
答案 1 :(得分:0)
最后我成功了!
我在自定义单元格中添加var ticketsurl: String!
,在ticketaction之前,现在看起来像这样:
var ticketsurl: String!
@IBAction func ticketsaction(sender: UIButton) {
let url = NSURL(string: "\(ticketsurl)")
UIApplication.sharedApplication().openURL(url!)
}
和PFQueryTableView Controller现在看起来像这样:
class Shows: PFQueryTableViewController {
override func queryForTable() -> PFQuery {
let query = PFQuery(className: "Shows")
query.orderByDescending("createdAt")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ShowsCell
let showplace = object?.objectForKey("Place") as? String
let showtickets = object?.objectForKey("Tickets") as? String
cell.place.text = showplace
cell.tickets.tag = indexPath.row
cell.ticketsurl = showtickets
return cell
}
}