![](/img/trans.png)
[英]Swift: Click onto cell of UICollectionView and open AlertViewController
[英]Open a Viewcontroller on cell click - Swift
我正在嘗試打開一個新的視圖控制器,它有一個文本字段,其中包含在 UITableView 中單擊的單元格文本。 雖然我已經發現了如何獲取被點擊單元格的信息,但我不知道如何將該值推送到新的 ViewController。 這是我當前的代碼:
class NearbyViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
@IBOutlet weak var nearbyTableView: UITableView!
var myList: [String] = []
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()
}
非常感謝
一些應該對您有所幫助的操場代碼。 使用didSelectRowAt
您需要的參數調用openDetails
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
}
}
在您的didSelectIndexPath
方法中,您正在訪問一個單元格並設置其標簽的文本。
你想要做的是:
let viewController = ViewController()
viewController.textFieldText = cell.textLabel.text
navigationController?.pushViewController(viewController, animated: true)
然后,一旦您在 ViewController 中有textFieldText
,您就可以說:
textField.text = textFieldText
嘗試:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") -> not correct
let cell = tableView. cellForRowAt(indexpath) // but don't need cell
let (cellName) = myList[indexPath.row]
let viewController = ViewController()
viewController.textField.Text = cellName
self.navigationController?.pushViewController(viewController, animated: true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.