gpt4 book ai didi

ios - 将自定义 tableviewcell 连接到 Uitableview

转载 作者:行者123 更新时间:2023-11-28 08:26:28 26 4
gpt4 key购买 nike

我是编程新手,所以我真的需要你的帮助。基本上我有非常基本的 TableView Controller ,里面有自定义单元格。

import UIKit

class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]

var restaurantImages = ["cafedeadend.jpg", "homei.jpg", "teakha.jpg", "cafeloisl.jpg", "petiteoyster.jpg", "forkeerestaurant.jpg", "posatelier.jpg", "bourkestreetbakery.jpg", "haighschocolate.jpg", "palominoespresso.jpg", "upstate.jpg", "traif.jpg", "grahamavenuemeats.jpg", "wafflewolf.jpg", "fiveleaves.jpg", "cafelore.jpg", "confessional.jpg", "barrafina.jpg", "donostia.jpg", "royaloak.jpg", "thaicafe.jpg"]

var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]

var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]

override func viewDidLoad() {
super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell

// Configure the cell...
cell.nameLabel.text = restaurantNames[(indexPath as NSIndexPath).row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[(indexPath as NSIndexPath).row])
cell.locationLabel.text = restaurantLocations[(indexPath as NSIndexPath).row]
cell.typeLabel.text = restaurantTypes[(indexPath as NSIndexPath).row]


return cell

我还有 4 个 sep TableView Controller 。我的问题是如何建立 segues,所以当我按下第一个单元格时 - “a”tableviewcontroller 打开。当我按下第二个单元格时 - “b”tableviewcontroller 打开。

我在编程方面非常新,所以请大家一步一步地编写所有内容。十分感谢你的帮助!!!

最佳答案

其他响应是为 Swift2 开发的,但您想要它用于 Swift3,这就是您找不到方法的原因。

因此,我将为您提供一个在 TableViewController 之间导航的非常基本的示例,希望它可以帮助您了解导航的工作原理,以便您在项目中实现它。

结果

代码

class TableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Go to Table \(indexPath.row + 1)"
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let numberOfRow = indexPath.row

switch numberOfRow{
case 0:
performSegue(withIdentifier: "showA", sender: numberOfRow)
case 1:
performSegue(withIdentifier: "showB", sender: numberOfRow)
default:
break;
}
}

// This method will be useful to you
// in case you want to send any info
// to your destination view controllers.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showA" {
let tableViewControllerA = segue.destination as! TableViewControllerA
tableViewControllerA.someData = sender as? Int
} else if segue.identifier == "showB" {
let tableViewControllerB = segue.destination as! TableViewControllerB
tableViewControllerB.someData = sender as? Int
}
}
}

class TableViewControllerA: UITableViewController {
var someData: Int?

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.title = "Table 1"
}
}

class TableViewControllerB: UITableViewController {
var someData: Int?

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.title = "Table 2"
}
}

附加信息:

enter image description here


操作方法如下:

enter image description here


enter image description here


enter image description here


enter image description here

关于ios - 将自定义 tableviewcell 连接到 Uitableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39829696/

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