2016-03-18 8 views
0

コンテンツを表示するためにUICollectionViewを利用するアプリケーションを開発中です。私はセルまたはラベルのいずれかをサファリで開くリンク(http)にしたいと思っています。Swift:UIcollectionViewの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") 

} 

答えて

0

最良の方法は属性付きの文字列とUITextViewを使用することです:

SWIFT 2.1

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

yourTextView.attributedText = attributedString 

uはUILabelのためにこのライブラリを試すことができます:https://github.com/AliSoftware/OHAttributedStringAdditions/wiki/link-in-UILabel

0

私はUITextViewを使用することをお勧めしますショーとハンドルは、リンクをタップします。 コードである必要があり、IBの内側にUITextViewのデリゲートを設定し、これは

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool { 
     return true 
} 

FUNC実装するために必要だとIB enter image description here

にリンク検出を有効にすることを忘れないでくださいまた

@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 
} 

関連する問題