我正在设计一个项目,我希望它在单击按钮时转到表格视图。
以下是故事板的图片: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 :(得分:0)
函数numberOfRowsInSection定义给定部分中的行数。如果定义0,则表必须为空。如果您定义了其他内容,则表视图会通过调用:
来请求每行的具体单元格func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
根据您的代码,首先查找崩溃原因。您的代码剪切不会显示它。