我正在尝试打开一个新的视图控制器,它具有一个文本字段,其中单元格的文本在UITableView中被单击。虽然我已经发现了如何获取点击的单元格信息,但我不知道如何将该值推送到新的ViewController。这是我目前的代码:
import UIKit
import Firebase
import FirebaseDatabase
class NearbyViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
@IBOutlet weak var nearbyTableView: UITableView!
var myList: [String] = []
var handle:DatabaseHandle?
var ref:DatabaseReference?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
var (cellName) = myList[indexPath.row]
cell.textLabel?.text = cellName
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
var (cellName) = myList[indexPath.row]
cell.textLabel?.text = cellName
print("row\(indexPath.row)")
print("name: \(cellName)")
}
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
handle = ref?.child("list").observe(.childAdded, with: { (snapshot) in
if let item = snapshot.value as? String
{
self.myList.append(item)
self.nearbyTableView.reloadData()
}
})
}
}
非常感谢
答案 0 :(得分:0)
一些应该帮助你的游乐场代码。
使用openDetails
所需的参数调用didSelectRowAt
func。
import UIKit
final class NearbyViewController: UIViewController {
// Your controller implementation
private func openDetails(cellName: String) {
let detailsController = DetailedInfoViewController() // OR use storyboard.instantiate
detailsController.cellName = cellName
// You must have a Navigaion stack for your controller
self.navigationController?.pushViewController(detailsController, animated: true)
}
}
final class DetailedInfoViewController: UIViewController {
var cellName: String!
// Your controller implementation
override func viewDidLoad() {
super.viewDidLoad()
// Set data into view outlets
}
}
答案 1 :(得分:0)
在您的didSelectIndexPath
方法中,您正在访问一个单元格并设置其标签的文本。
您想要做的是:
let viewController = ViewController()
viewController.textFieldText = cell.textLabel.text
navigationController?.pushViewController(viewController, animated: true)
然后,一旦你的ViewController中有textFieldText
,你就可以说:
textField.text = textFieldText
答案 2 :(得分:-1)
尝试:
map.setCenter(new google.maps.LatLng(lat, lng));