我有一个按钮,就是用户名。当我单击用户名时,我希望传递数据,还希望获取所选用户名的userid。我知道它不起作用,因为我知道从技术上讲,我没有选择一行。关于如何获得按钮的行索引的任何提示,谢谢。
这是我的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 = ""
}
发布于 2018-06-10 06:23:46
您需要将单元格作为参数传递给usernameClicked
委托方法。
然后,表格视图控制器可以询问表格视图单元格的索引路径是什么。然后,从索引路径中可以获得所需的数据。
更新协议:
protocol PostCellDelegate {
func usernameClicked(_ cell: PostTableViewCell)
}
更新按钮处理程序:
@IBAction func usernameButtonAction(_ sender: Any) {
print("Username clicked")
self.delegate?.usernameClicked(self)
}
将属性添加到您的` `DiscussionListTableViewController:
var clickedPath: IndexPath? = nil
更新视图控制器中的方法:
func usernameClicked(_ cell: PostTableViewCell) {
if let indexPath = self.tableView.indexPath(for: cell) {
clickedPath = indexPath
performSegue(withIdentifier: "viewProfile", sender: self)
}
}
然后,在您的prepare(for:)
中,替换为:
let indexPath = tableView.indexPathForSelectedRow!
通过以下方式:
if let indexPath = clickedPath {
// get the row
// access the posts data
}
https://stackoverflow.com/questions/50778968
复制相似问题