2016-03-18 3 views
0

Ich arbeite an einer App, die die UICollectionView verwendet, um Inhalte anzuzeigen. Ich möchte, dass entweder die Zelle oder das Label ein Link (http) ist, der in Safari geöffnet wird.Swift: Zelle oder Label ist ein http Link in einer UIcollectionView

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") 

} 

Antwort

0

würde ich empfehlen, UITextView zu verwenden, um Zeigen und handhaben tippen Sie auf den Link. -Code sollte die

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

folgende sein auch ist es erforderlich UITextView Delegierten innerhalb der IB und implementieren diese Func

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

und vergessen Sie nicht, damit Links Erkennung in der IB enter image description here

einstellen
Verwandte Themen