我正在使用SwiftyJSON从外部网站下载图像并在表格视图中显示它们。我想点击一个特定的图像,让它打开一个外部URL。怎么做到这一点?
基本上我通过按下按钮(如下所示),我想通过点击图像来做。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! myCell
let str = TableData[indexPath.row].thumbnail
let imagePath = (str.substringToIndex(str.startIndex.advancedBy((str.characters.count)-11)))+".png" //Removing "-100x90.png" from each thumbnail path and appending extension .png later on to fetch original sized image
cell.myImageView.kf_setImageWithURL(NSURL(string: imagePath)!)
cell.myLabel.text = String(TableData[indexPath.row].date) + ":" + String(TableData[indexPath.row].title)
print(TableData[indexPath.row].permalink)
cell.myButton.tag = indexPath.row
cell.myButton.addTarget(self, action: "openURL:", forControlEvents: UIControlEvents.TouchUpInside)
print(cell.myButton.tag)
return cell
}
func openURL(sender:UIButton){
print("Button Pressed")
UIApplication.sharedApplication().openURL(NSURL(string: TableData[sender.tag].permalink)!)
}
答案 0 :(得分:0)
将UITapGestureRecognizer
添加到单元格中的图像视图中:
let tapRecognizer = UITapGestureRecognizer(target: self, action: "openURL:")
cell.myImageView.addGestureRecognizer(tapRecognizer)
答案 1 :(得分:0)
它适用于以下代码:
let singleTap = UITapGestureRecognizer(target: self, action:"tapDetected:")
singleTap.numberOfTapsRequired = 1
cell.myImageView.userInteractionEnabled = true
cell.myImageView.tag = indexPath.row
cell.myImageView.addGestureRecognizer(singleTap)
func tapDetected(sender: UITapGestureRecognizer) {
UIApplication.sharedApplication().openURL(NSURL(string: TableData[(sender.view!.tag)].link)!)
}