У меня есть кнопка с именем пользователя.Когда я нажимаю на имя пользователя, я хочу передать данные, и я также хочу получить идентификатор пользователя выбранного имени пользователя.Я знаю, что это не работает, потому что я знаю, что технически я не выбираю строку.Любые советы о том, как получить индекс строки кнопки, спасибо.
Вот мой ViewController
import UIKit
import Firebase
import FirebaseAuth
import FirebaseStorage
import FirebaseDatabase
class DiscussionListTableViewController: UITableViewController, PostCellDelegate {
var postRef: DatabaseReference!
var storageRef: StorageReference!
var posts = [Posts]()
@IBAction func profileAction(_ sender: Any) {
if Auth.auth().currentUser == nil {
performSegue(withIdentifier: "toLogin", sender: self)
print("no user logged in")
} else {
performSegue(withIdentifier: "toProfile", sender: self)
}
}
override func viewDidAppear(_ animated: Bool) {
postRef = Database.database().reference().child("posts")
postRef.observe(DataEventType.value, with: { (snapshot) in
var newPosts = [Posts]()
for post in snapshot.children {
let post = Posts(snapshot: post as! DataSnapshot)
newPosts.insert(post, at: 0)
}
self.posts = newPosts
self.tableView.reloadData()
}, withCancel: {(error) in
print(error.localizedDescription)
})
}
override func viewDidLoad() {
tableView.rowHeight = 160
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
cell.usernameLabel.setTitle(posts[indexPath.row].username, for: .normal)
cell.postDescriptionLabel.text = posts[indexPath.row].description
cell.postTitleLabel.text = posts[indexPath.row].title
let image = posts[indexPath.row]
cell.userProfilePic.sd_setImage(with: URL(string: image.userImageStringUrl), placeholderImage: UIImage(named: "1"))
cell.postImageView.sd_setImage(with: URL(string: image.postImageStringUrl), placeholderImage: UIImage(named: "1"))
cell.delegate = self
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "addComment", sender: self)
}
func usernameClicked() {
performSegue(withIdentifier: "viewProfile", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addComment" {
let commentVC = segue.destination as! CommentTableViewController
let indexPath = tableView.indexPathForSelectedRow!
let selectedIndex = posts[indexPath.row]
commentVC.selectedPost = posts[indexPath.row]
commentVC.postDescription = selectedIndex.description
commentVC.postImageUrl = selectedIndex.postImageStringUrl
commentVC.postTitle = selectedIndex.title
commentVC.postUsername = selectedIndex.username
commentVC.profilePicUrl = selectedIndex.userImageStringUrl
} else {
if segue.identifier == "viewProfile" {
let viewProfileVC = segue.destination as? ViewProfileViewController
let indexPath = tableView.indexPathForSelectedRow
let selectedIndex = posts[indexPath.row]
viewProfileVC?.username = selectedIndex.username
viewProfileVC?.userid = selectedIndex.userid
}
}
}
}
Я думаю, что моя ошибка здесь, поскольку я не выбираю строку, когдаЯ нажимаю кнопку:
if segue.identifier == "viewProfile" {
let viewProfileVC = segue.destination as? ViewProfileViewController
let indexPath = tableView.indexPathForSelectedRow
let selectedIndex = posts[indexPath.row]
viewProfileVC?.username = selectedIndex.username
viewProfileVC?.userid = selectedIndex.userid
}
Вот мой TableViewCell
import UIKit
import Foundation
protocol PostCellDelegate {
func usernameClicked()
}
class PostTableViewCell: UITableViewCell {
var delegate: PostCellDelegate?
@IBOutlet weak var userProfilePic: UIImageView!
@IBOutlet weak var usernameLabel: UIButton!
@IBOutlet weak var postImageView: UIImageView!
@IBOutlet weak var postDescriptionLabel: UILabel!
@IBOutlet weak var postTitleLabel: UILabel!
@IBAction func usernameButtonAction(_ sender: Any) {
print("Username clicked")
self.delegate?.usernameClicked()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Вот контроллер представления, в который я пытаюсь передать данные.
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage
import FirebaseAuth
class ViewProfileViewController: UIViewController {
var clickedUserRef: DatabaseReference!
var userid = ""
var username = ""
}