我想从表视图的选定单元格中检索文本,以在其他视图控制器中修改标签。 我的第一个问题是,我无法成功获取值,并将其放置后
这是我的tableview文件:
Foos
我的帖子文件:
class SelectionMarqueViewController: UITableViewController {
var posts = [Post]()
var indexMarques = 0
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
loadMarques()
}
func loadMarques() {
var ref : DatabaseReference?
ref = Database.database(url: "https://myride-test.firebaseio.com/").reference()
ref?.observe(.childAdded, with: { (snapshot) in
if let dict = snapshot.value as? [String: Any] {
let marqueText = dict["Marque"] as! String
let post = Post(marqueText: marqueText)
self.posts.append(post)
//print(self.posts)
self.tableView.reloadData()
}
if let dict = snapshot.value as? [String: Any] {
let marqueText = dict["Marque"] as! String
let post = Post(marqueText: marqueText)
self.posts.append(post)
self.posts = Array(Set(self.posts))
self.tableView.reloadData()
}
})
}
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)
let orderPosts = self.posts.sorted { $0.marque < $1.marque}
cell.textLabel?.text = orderPosts[indexPath.row].marque
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
indexMarques = indexPath.row
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var selectionMarque = segue.destination as! ChoiceGeneralTableViewController
}
}
然后在我的视图文件中必须更改标签:
class Post : Hashable {
var marque: String
init(marqueText: String) {
marque = marqueText
}
// Equatable for contains
static func == (lhs:Post,rhs:Post) -> Bool {
return lhs.marque == rhs.marque
}
// Hashable for Set
var hashValue:Int {
return marque.hashValue
}
}
实际上我没有成功检索单元格的文本,self.posts返回ProjectName.Post
感谢您的研究和帮助!
答案 0 :(得分:0)
向ChoiceGeneralTableviewController添加变量:
public var postText: String?
实例化selectionMarque(另一个表视图控制器)后,立即在表视图文件(第一个)didSelectRowAtIndexPath(...)中:
selectionMarque.postText = orderPosts[indexMarques].marque
在ChoiceGeneralTableviewController中,您可以访问postText作为标签的文本。
编辑:代码
class SelectionMarqueViewController: UITableViewController {
var posts = [Post]()
var indexMarques = 0
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
loadMarques()
}
func loadMarques() {
var ref : DatabaseReference?
ref = Database.database(url: "https://myride-test.firebaseio.com/").reference()
ref?.observe(.childAdded, with: { (snapshot) in
if let dict = snapshot.value as? [String: Any] {
let marqueText = dict["Marque"] as! String
let post = Post(marqueText: marqueText)
self.posts.append(post)
//print(self.posts)
self.tableView.reloadData()
}
if let dict = snapshot.value as? [String: Any] {
let marqueText = dict["Marque"] as! String
let post = Post(marqueText: marqueText)
self.posts.append(post)
self.posts = Array(Set(self.posts))
self.tableView.reloadData()
}
})
// Sort the posts
self.posts = self.posts.sorted { $0.marque < $1.marque}
}
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)
cell.textLabel?.text = self.posts[indexPath.row].marque
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
indexMarques = indexPath.row
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var selectionMarque = segue.destination as! ChoiceGeneralTableViewController
selectionMarque.postText = posts[indexMarques].marque
}
}
和目标视图控制器:
class ChoiceGeneralTableViewController: UITableViewController {
@IBOutlet weak var marquePressed: UILabel!
@IBOutlet weak var labelMarque: UILabel!
public var postText: String?
var marqueSelected = posts[indexMarques]
override func viewDidLoad() {
super.viewDidLoad()
// Make sure postText is not nil
if let text = postText {
labelMarque.text = text
}
}
}
答案 1 :(得分:0)
您可以使用tableView.indexPath(for:)
方法获取发件人索引路径
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = tableView.indexPath(for: sender as UITableViewCell);
var selectionMarque = segue.destination as! ChoiceGeneralTableViewController
selectionMarque.selected = orderPosts[indexPath.row]
}
然后,您需要将posts[indexMarques]
更改为: Post!
,因为该类无权访问posts数组,我们需要直接传递所选的Post对象。
class ChoiceGeneralTableViewController: UITableViewController {
@IBOutlet weak var marquePressed: UILabel!
@IBOutlet weak var labelMarque: UILabel!
var selected: Post!
override func viewDidLoad() {
super.viewDidLoad()
labelMarque.text = selected.marque
}
}