2017-04-12 56 views
0

如何在表格中添加標籤查看識別URLS和鏈接將它們發佈到textField中時?UiLabel識別TableViewCell中的URL Swift

enter image description here

我怎樣才能得到MessageLabel閱讀和理解在tableViewCell鏈接和顯示鏈接?

struct postStruct { 
    let username : String! 
    let message : String! 
    let photoURL : String! 
    let timeStamp: String! 
    let cellUserId : String! 
} 



class GeneralChatroom: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate { 

    @IBOutlet weak var messageTextField: UITextField! 
    @IBOutlet weak var tableView: UITableView! 

    var generalRoomDataArr = [postStruct]() 



    override func viewDidLoad() { 
     super.viewDidLoad() 

     messageTextField.delegate = self 

     self.tableView.dataSource = self 
     self.tableView.delegate = self 
     self.tableView.estimatedRowHeight = 78 
     self.tableView.rowHeight = UITableViewAutomaticDimension 

      //Assign array values 
      self.generalRoomDataArr.insert(postStruct(username: username, message: message, photoURL: firebaseUserPhotoURL, timeStamp: timeStamp, cellUserId: userIDString), at: 0) 

    }//End ViewDidLoad 





    //MARK:      Buttons (BackButton || Send Button) 


    @IBAction func backButtonPressed(_ sender: UIButton) { 
     self.performSegue(withIdentifier: "BackToRoom", sender: nil) 
    } 


    //Message Send button is pressed data uploaded to firebase 
    @IBAction func sendButtonPressed(_ sender: UIButton) { 

     //If a character exists will be uploaded to firebase 
     if ((messageTextField.text?.characters.count)! > 0) { 

     let message : String = self.messageTextField.text! 
     UploadGeneralChatRoom(message: message) //upload to general_room 
     self.messageTextField.text = nil 
     messageTextField.resignFirstResponder()//Quit keyboard 

     self.tableView.reloadData() //Reload tableView 
     //UploadUserData() //Update Rank in database 

     } 


    } 



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return generalRoomDataArr.count // your number of cell here 
    } 




    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

     //Transform Data From^to load at the bottom 
     tableView.transform = CGAffineTransform (scaleX: 1,y: -1); 
     cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1); 
     cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1); 

     //Set username label to display username 
     let usernameLabel = cell?.viewWithTag(1) as! UILabel 
     usernameLabel.text = generalRoomDataArr[indexPath.row].username 


     //Set message label to display message 
     let messageLabel = cell?.viewWithTag(2) as! UILabel 
     messageLabel.text = generalRoomDataArr[indexPath.row].message 
     messageLabel.numberOfLines = 0 

     //initialize UI Profile Image 
     let imageView = cell?.viewWithTag(3) as! UIImageView 

     //Make Porfile Image Cirlce 
     imageView.layer.cornerRadius = imageView.frame.size.width/2 
     imageView.clipsToBounds = true 



     //Set timeStampLabel to current time AGO 
     let timeStampLabel = cell?.viewWithTag(4) as! UILabel 
     timeStampLabel.text = generalRoomDataArr[indexPath.row].timeStamp 
     timeStampLabel.numberOfLines = 0 


     return cell! 
    } 



    func checkCurrentPhoto() { 
     // let oldImageString = index.row 


    } 



    func TappedOnImage(sender: UITapGestureRecognizer){ 
     print("Elltappy") 
    } 


}//END CLASS 

回答

2

你必須使用UITextView,而不是作爲UILabel在這篇文章中描述:How to intercept click on link in UITextView?

+0

他這個多少錢去改變我的代碼?我需要它是一個標籤 – codechicksrock

+0

'UITextView'是唯一內置的UI元素,可以讓你點擊URL鏈接。如果它必須是一個標籤,那麼它將不起作用。 –