2017-04-24 93 views
0

我需要调用此函数。我需要收到用户identityString。我该如何去做这件事?调用另一个类中的函数Swift

class GeneralChatroom: UIViewController, 
         UITableViewDataSource, 
         UITableViewDelegate, 
         UITextFieldDelegate, 
         UITextViewDelegate { 

    //Get Data of current cell that has been tapped 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {  
     let userIdentityString = generalRoomDataArr[indexPath.row].cellUserId 

     print("Uid of cell Data: " + userIdentityString!) 
     print("section: \(indexPath.section)") 
     print("row: \(indexPath.row)") 
    } 

    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 mesage TextView Label to display message in textView 
    let messageLabel = cell?.viewWithTag(5) as! UITextView 
    messageLabel.text = generalRoomDataArr[indexPath.row].message 

    //TO DO: dont know if this actually works prob can delete 
    messageLabel.setContentOffset(CGPoint(x: 0, y: 0), animated: false) 


    //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 


    //Loading and change of Usesrs profile image on chat cell 
    let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL 

    //Load profile image(on cell) with URL & Alamofire Library 
     let downloadURL = NSURL(string: userProfileChatImage!) 
     imageView.af_setImage(withURL: downloadURL as! URL) 

    return cell! 
} 

} 

但我需要在不同的类中调用该函数才能获得userIdentityString。我怎样才能做到这一点?我需要在Image Tapped功能中调用它。

class GeneralChatroomTableViewCell: UITableViewCell { 

    @IBOutlet weak var profileImageView: UIImageView! 
    @IBOutlet weak var textViewHeight: NSLayoutConstraint! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     // You must to use interaction enabled 
     profileImageView.isUserInteractionEnabled = true 
     profileImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:)))) 
    } 

    func imageTapped(_ sender: UITapGestureRecognizer) { 
     //first you need to call the function that reads the cell. Recieve the UID 
     print("image tapped") 
    } 
} 

也许这将是更清晰

func imageTapped(_ sender: UITapGestureRecognizer) { 

     let userIDString = GeneralChatroom.tableView(userIdentityString: userIdentityString) 

     print(userIDString) 

     //first you need to call the function that reads the cell. Recieve the UID 
     print("image tapped") 


    } 



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) -> String{ 

     let userIdentityString = generalRoomDataArr[indexPath.row].cellUserId 

     print("Uid of cell Data: " + userIdentityString!) 
     print("section: \(indexPath.section)") 
     print("row: \(indexPath.row)") 

     //1.) If imageView touch is deteected 
     //2.) Segua to new view controller by passing in the string UID(userIdentityString) of the cell 
     //3.) Get database information based on the UID that is added(look at prevous methods) 
     //  -might have to call this function and use separeate function 
     //4.) Output data from database Users to represent a user profile 



     //All you have to do is pass a UID (Check other Database methods to send UID) 

     return userIdentityString! 

    } 
+1

您在创建单元时是否有这些数据?为什么不创建一个属性将其存储在实际单元格上,以便它始终可用。 – AtheistP3ace

+0

你能告诉我一个例子吗? – codechicksrockk

+0

首先你要调用的函数是一个委托函数。这是自动调用的,所以手动调用它可能会有无法预料的值。你使用的故事板看起来像我不太熟悉,但我会在'func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {'委托函数每个调用当视图使用UITableViewDelegate和UITableViewDataSource时,会在表中创建一个单元格。 – AtheistP3ace

回答

0

OK,正如我在评论这个回答是不是从迅速类调用另一个函数,因为你正在试图调用委托函数说明。我不确定如果手动调用,函数将具有预期的值。我会做的是在你的TableViewCell上创建一个属性来保存userId

class GeneralChatroomTableViewCell: UITableViewCell { 

    @IBOutlet weak var profileImageView: UIImageView! 
    @IBOutlet weak var textViewHeight: NSLayoutConstraint! 

    // User Id 
    var userId: String? 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     // You must to use interaction enabled 
     profileImageView.isUserInteractionEnabled = true 
     profileImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:)))) 
    } 

    func imageTapped(_ sender: UITapGestureRecognizer) { 

     //first you need to call the function that reads the cell. Recieve the UID 
     print("image tapped") 
     if let uid = self.userId { 
      // Do whatever you want with userId 
     } 
    } 
} 

所以我在TableViewCell上添加了一个可选的userId属性。为了填充,我们将在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {委托函数中执行此操作。我不确定你的看起来像什么,因为你没有提供这个代码,所以我只是继续看它的一般情况,以及你的GeneralChatroomTableViewCell是什么。

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

    var cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(GeneralChatroomTableViewCell.self), for: indexPath) as! GeneralChatroomTableViewCell 
    (cell as! GeneralChatroomTableViewCell).userId = generalRoomDataArr[indexPath.row].cellUserId 
    return cell 
} 

无论何时创建单元格以在表格中显示,都会调用此函数。所以现在创建单元格,我们设置了userId,以便在我们需要它时始终可用。

而且正如上面的评论所述,这只有在数据在创建单元格时可用时才有用。

+0

这不会对我有用我需要以某种方式调用函数,如我上面所述。我不能这样做。对不起,我没有发布我的所有代码。 – codechicksrockk

+0

您可以手动调用该函数,但需要提供所需单元格的indexPath。有没有理由不能做到以上?如果没有所有代码,都很难知道表代理中正在发生的任何事情。如果我理解了我的限制,我可以提供更好的解决方案。 – AtheistP3ace

+0

我所要做的就是将userIdentity String转换为imageTapped函数。 – codechicksrockk

相关问题