2016-09-19 11 views
0

私はレストランクラスを作成し、セルを表示するためにすべての接続を行いましたが、実行時には、作成したすべてのアウトレットが表示されません。あなたが必要RestaurantTableTableViewにカスタムクラスを設定したインターフェイスビルダーで、テーブルビューにセルスウィフトが表示されない

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

はここだねえ、私はあなたの問題を考え出し

my github repo

+0

あなたはテーブルビューのデリゲートを設定しましたか?デバッグで実行すると、cellForRowAtのコードにindexPathが実行されていますか? – lubilis

+0

tableView.reloadData()? – Suhaib

答えて

1

:「ResturantTableTableViewController.swift」ちょうどあなたのファイルのように

ResturantTableTableViewController 

が、このファイル内に、あなたのようにそれを宣言した:

class RestaurantTableViewController: UITableViewController { 

ストーリーボードはファイル名を無視しますが、ファイル内で宣言した名前を使用します。

ファイル名を変更し、ストーリーボードを同じクラスにリンクします。または、 "ResturantTableTableViewController.swift"ファイル内のクラス名をファイル名( "ResturantTableTableViewController")と同じに変更することもできます。

enter image description here

enter image description here

+0

なぜdownvote?あなたのプロジェクトをダウンロードして、自分で試しました。ストーリーボードでクラスを変更すると、セルが表示されます。 – LaStrada

0

私githuレポ:ここ

は私のコードです実際のファイル名は RestaurantTableViewControllerです。

あなたに「バーガーレストラン」テーブルコントローラをリンクされているストーリーボードで
関連する問題