Xcode Swift:单元格或标签必须是UIcollectionView中的http链接

Xcode Swift:单元格或标签必须是UIcollectionView中的http链接,xcode,swift,uicollectionview,Xcode,Swift,Uicollectionview,我正在开发一个应用程序,它将利用UICollectionView来显示内容。我想让单元格或标签成为将在safari中打开的链接(http) import UIKit class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { var tableData: [String] = ["Data1", "Data2", "Data3"] var tableImages

我正在开发一个应用程序,它将利用UICollectionView来显示内容。我想让单元格或标签成为将在safari中打开的链接(http)

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate  {

var tableData: [String] = ["Data1", "Data2", "Data3"]
var tableImages: [String] = ["img1.png", "img2.jpg", "img3.jpg"]






override func viewDidLoad() {




    // Do any additional setup after loading the view, typically from a nib.
}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return tableData.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colvwCell

    cell.lblCell.text = tableData[indexPath.row]
    cell.imgCell.image = UIImage(named: tableImages [indexPath.row])
    return cell

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Cell \(indexPath.row) selected")

}

最好的方法是将UITextView与属性字符串一起使用:

swift 2.1

var str = "Google"
var attributedString = NSMutableAttributedString(string:str, attributes:[NSLinkAttributeName: NSURL(string: "http://www.google.com")!])

yourTextView.attributedText = attributedString

您可以尝试使用此UILabel库:

我建议使用UITextView来显示和处理链接。 代码应该如下所示

@IBOutlet private weak var textView: UITextView!

override func viewDidLoad() {
     super.viewDidLoad()
     textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.blueColor()]

     let text = NSMutableAttributedString()
     text.beginEditing()
     text.appendAttributedString(NSAttributedString(string: "google", attributes: [NSLinkAttributeName: NSURL(string: "http://www.google.com")!]))
     text.endEditing()

     textView.attributedText = text
}
此外,还需要在IB内设置UITextView委托并实现此功能

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
        return true
}
不要忘记在IB中启用链接检测