我需要调用此函数。我需要收到用户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!
}
您在创建单元时是否有这些数据?为什么不创建一个属性将其存储在实际单元格上,以便它始终可用。 – AtheistP3ace
你能告诉我一个例子吗? – codechicksrockk
首先你要调用的函数是一个委托函数。这是自动调用的,所以手动调用它可能会有无法预料的值。你使用的故事板看起来像我不太熟悉,但我会在'func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {'委托函数每个调用当视图使用UITableViewDelegate和UITableViewDataSource时,会在表中创建一个单元格。 – AtheistP3ace