gpt4 book ai didi

ios - 调用另一个 Swift 类中的函数

转载 作者:行者123 更新时间:2023-11-30 12:30:40 25 4
gpt4 key购买 nike

我需要调用这个函数。我需要接收用户identityString。我怎样才能做到这一点?

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。我怎样才能做到这一点?我需要在“图像点击”函数中调用它。

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!

}

最佳答案

好的,正如我在评论中所说,这个答案不是关于从 swift 类调用另一个函数,因为您正在尝试调用委托(delegate)函数。我不确定是否可以保证如果手动调用该函数将具有预期值。我要做的就是在您的 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 { 中执行此操作。委托(delegate)功能。我不确定你的是什么样子,因为你没有提供该代码,所以我只是继续了解它通常是什么样子以及你的 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这样我们就可以在需要时随时使用它。

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

关于ios - 调用另一个 Swift 类中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43594145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com