简体   繁体   English

具有超链接的Swift 2表格视图单元

[英]Swift 2 Table View Cell with Hyperlink

I've created a UITableView with different Sections and cells like: "Follow us on Instagram" or "Like us on Facebook". 我创建了一个具有不同部分和单元格的UITableView ,例如:“在Instagram上关注我们”或“在Facebook上喜欢我们”。 This cells should have a link to each page. 该单元格应具有指向每个页面的链接。 I tried this: 我尝试了这个:

@IBAction func WebLink(sender: AnyObject) {
if let url = NSURL(string: "http://...") {
    UIApplication.sharedApplication().openURL(url)
}
}

But i can't link the @IBAction with the Cell... 但是我无法将@IBAction与Cell链接起来...

It should look like this and on every Cell should be a Hyperlink. 它看起来应该像这样,并且在每个Cell上都应该是一个超链接。

在此处输入图片说明

You can directly use: 您可以直接使用:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
     // launch your func WebLink with the currentCell
     ...
}

I'm not finding anything that would allow us to do so with a simple label. 我找不到任何可以让我们使用简单标签来做的事情。 But there is a library available that does so in github. 但是github中有一个可用的库。 It seems you can't do so with a simple label in Swift. 在Swift中,使用简单的标签似乎无法做到这一点。 However, there is a library called TTTAttributedLabel that I think does what you're looking for. 但是,我认为有一个名为TTTAttributedLabel的库可以满足您的需求。 Here's a link to the library: https://github.com/TTTAttributedLabel/TTTAttributedLabel 这是库的链接: https : //github.com/TTTAttributedLabel/TTTAttributedLabel

@IBOutlet var exampleLabel: TTTAttributedLabel!

    //...

let exampleString: NSString = "My super cool link"
exampleLabel.delegate = self
exampleLabel.text = exampleString as String
var range : NSRange = exampleString.rangeOfString("link")
exampleLabel.addLinkToURL(NSURL(string: "http://www.stackoverflow.com")!, withRange: range)

func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
        UIApplication.sharedApplication().openURL(url)
}

Side note: Did not ever imagine I'd miss Objective-C. 旁注:从未想象过我会错过Objective-C。 I'll update with a Swift 3.0 answer if this is what you were looking for. 如果这是您要的内容,我将用Swift 3.0答案进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM