ios - TableView 不显示 Cells Swift

标签 ios iphone swift swift2 swift3

我创建了一个 Restaurant 类并建立了所有连接以显示单元格,但是当我点击运行时,它没有显示我创建的所有导出。

这是我的代码:

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]
            }
        }
    }
}

这是我的 githu 仓库

my github repo

最佳答案

在 Storyboard 中,您将“汉堡餐厅”餐 table Controller 链接到:

ResturantTableTableViewController

就像您的文件“ResturantTableTableViewController.swift”一样,但是在这个文件中,您已将其声明为:

class RestaurantTableViewController: UITableViewController {

Storyboard忽略文件名,但使用您在文件中声明的名称。

将文件名和链接 Storyboard更改为同一类。或者您可以将“ResturantTableTableViewController.swift”文件中的类名更改为与文件名(“ResturantTableTableViewController”)相同的名称。

enter image description here

enter image description here

关于ios - TableView 不显示 Cells Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39575492/

相关文章:

ios - 我的 View Controller 文件太长,如何将一些功能放在单独的 swift 文件中

objective-c - 如果未链接到 XCode 或 Instrument,应用程序会崩溃

ios - Swift 以编程方式添加约束

iphone - 在iPhone中的SQLite3中编写插入查询

iphone - 如何调用performSelectorOnMainThread : with an selector that takes > 1 arguments?

swift - View Controller 内的简单数据传输,无需转换

ios - 为什么内部类出现在 ProductName-Swift.h 中?我可以手动更改此文件吗?

ios - 脂肪错误!!无法打开输入文件

ios - MFMailComposeViewController 在设备 8.2 上崩溃

ios - Iphone(ios 5.0) 低功耗蓝牙 : can not get data from the device