Calling a function in another class Swift

时间:2017-04-24 17:15:05

标签: ios swift function swift3

I need to call this function. I need to receive the user identityString. How can i go about doing this?

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

}

But I need to call that function in a different class to get the userIdentityString. How can I do this? I need to call it in the Image Tapped function.

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

maybe this will be more clear

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 个答案:

答案 0 :(得分:0)

好的,正如我在评论中所述,这个答案不是关于从swift类中调用另一个函数,因为你试图调用一个委托函数。我不确定如果手动调用,可以保证函数具有预期值。我要做的是在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,以便在需要时始终可以使用它。

正如上面的评论中所述,只有在创建单元格时可以获得该数据时,这才有用。