[英]Table View not showing the Cells Swift
I have created a Restaurant class and made all the connections to display the cells but when i hit run , it doesn't show the all the outlets that i have created. 我创建了一个Restaurant类,并进行了所有连接以显示单元格,但是当我点击run时,它并没有显示我创建的所有网点。
Here's my code: 这是我的代码:
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurants:[Resturant] = [
Resturant(name: "Hardees", type: "Burger", location: "Many", image: "Hardees.jpg", isVisited: false), Resturant(name: "Shake Shack", type: "Burger", location: "Gulf Mall", image: "shakeshack.jpg", isVisited: false), Resturant(name: "Burgery", type: "Burger", location: "Gulf Mall", image: "shakeshack.jpg", isVisited: false), Resturant(name: "Dairy Queen", type: "Burger", location: "Gulf Mall", image: "shakeshack.jpg", isVisited: false), Resturant(name: "Elevation Burger", type: "Burger", location: "Gulf Mall", image: "shakeshack.jpg", isVisited: false), Resturant(name: "Fat Burger", type: "Burger", location: "Gulf Mall", image: "shakeshack.jpg", isVisited: false)
]
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 restaurants.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurants[indexPath.row].name
cell.thumbnailImageView.image = UIImage(named: restaurants[indexPath.row].image)
cell.locationLabel.text = restaurants[indexPath.row].location
cell.typeLabel.text = restaurants[indexPath.row].type
cell.accessoryType = restaurants[indexPath.row].isVisited ? .checkmark : .none
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
restaurants.remove(at: indexPath.row)
}
tableView.deleteRows(at: [indexPath], with: .fade)
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
// Social Sharing Button
let shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Share", handler: { (action, indexPath) -> Void in
let defaultText = "Just checking in at " + self.restaurants[indexPath.row].name
if let imageToShare = UIImage(named: self.restaurants[indexPath.row].image) {
let activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
}
})
// Delete button
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Delete",handler: { (action, indexPath) -> Void in
// Delete the row from the data source
self.restaurants.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
})
shareAction.backgroundColor = UIColor(red: 48.0/255.0, green: 173.0/255.0, blue: 99.0/255.0, alpha: 1.0)
deleteAction.backgroundColor = UIColor(red: 202.0/255.0, green: 202.0/255.0, blue: 203.0/255.0, alpha: 1.0)
return [deleteAction, shareAction]
}
// MARK: -
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showRestaurantDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destination as! ResturantDetailViewController
destinationController.resturant = restaurants[indexPath.row]
}
}
}
}
here's my githu repo 这是我的githu回购
In the Storyboard you are linking the "Burger restaurant" table controller to: 在情节提要中,您将“汉堡餐厅”表控制器链接到:
ResturantTableTableViewController
Just like your file "ResturantTableTableViewController.swift", but inside this file, you have declared it as: 就像文件“ ResturantTableTableViewViewController.swift”一样,但是在此文件中,您将其声明为:
class RestaurantTableViewController: UITableViewController {
The storyboard ignores the file-name, but uses the name you have declared inside the file. 故事板将忽略文件名,但使用您在文件内部声明的名称。
Change the file-name and link storyboard to the same class. 更改文件名并将情节提要链接到同一类。 Or you can change the class-name inside the "ResturantTableTableViewController.swift" file to the same as the file name ("ResturantTableTableViewController").
或者,您可以将“ ResturantTableTableViewController.swift”文件中的类名称更改为与文件名相同的名称(“ ResturantTableTableTableViewController”)。
嘿,我想通了您的问题,在Interface Builder你的自定义类设置为RestaurantTableTableView
当您需要将其设置为你实际文件名RestaurantTableViewController
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.