gpt4 book ai didi

ios - 使用单击的按钮通过 tableView 传递数据

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

我有一个按钮,就是用户名。当我点击用户名时,我想传递数据,我也想抓取所选用户名的用户标识。我知道它不起作用,因为我知道我在技术上没有选择一行。关于如何获取按钮的行索引的任何提示,谢谢。

这是我的 View Controller

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

这是我试图将数据传递到的 View Controller 。

import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage
import FirebaseAuth

class ViewProfileViewController: UIViewController {
var clickedUserRef: DatabaseReference!
var userid = ""
var username = ""
}

最佳答案

您需要将单元格作为参数传递给 usernameClicked 委托(delegate)方法。

然后 TableView Controller 可以询问 TableView 单元格的索引路径是什么。然后从索引路径中你可以得到你需要的数据。

更新协议(protocol):

protocol PostCellDelegate {
func usernameClicked(_ cell: PostTableViewCell)
}

更新按钮处理程序:

@IBAction func usernameButtonAction(_ sender: Any) {
print("Username clicked")
self.delegate?.usernameClicked(self)
}

向您的“DiscussionListTableViewController”添加一个属性:

var clickedPath: IndexPath? = nil

更新 View Controller 中的方法:

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
}

关于ios - 使用单击的按钮通过 tableView 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50778968/

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