2017-04-16 25 views
-1

我正在設計一個項目,我希望它在單擊按鈕時轉到表視圖。Xcode的UIButton UITableView無法連接

這裏是故事板的一個圖:Storyboard

在這裏被用於與該按鈕的視圖的代碼:

import UIKit 
import os.log 

class MealViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    //MARK: Properties 
    @IBOutlet weak var nameLabel: UILabel! 
    @IBOutlet weak var priceLabel: UILabel! 
    @IBOutlet weak var photoImageView: UIImageView! 
    @IBOutlet weak var ratingControl: RatingControl! 
    @IBOutlet weak var caloriesLabel: UILabel! 
    @IBOutlet weak var descriptionLabel: UILabel! 


    @IBAction func addToCart(_ sender: UIButton) { 
    } 

和下面是表視圖代碼:

import UIKit 

class CartTableViewController: UITableViewController { 

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

當我運行它時,它會打開一張空白表。當我更改numberOfRowsInSection時,它會發出sigabrt錯誤。我如何才能定期打開視圖?

回答

0

函數numberOfRowsInSection定義給定部分中的行數。如果您定義0,表格必須爲空。如果通過調用定義別的表視圖請求於各行的具體電池:

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

根據代碼那尋找墜機原因的首位。你的代碼被剪斷不會顯示出來。