UITableView datasource functions not called (swift)

余生长醉 提交于 2019-12-13 01:51:20

问题


So I had this working a hot second ago, but for some reason it's stopped working and the tableview just appears blank.

I set the Detail View Controller as a UITableViewDataSource and UITableViewDelegate and did tableview.datasource = self as well as for the delegate but the methods are just not called. It's builds and runs and everything though.

It stopped working when I changed the style to grouped, I'm not sure if that has anything to do with it although I can't see a problem in the code.

Thanks!

Here is my Detail VC code. It's not totally complete yet as far as other methods go but the table view should be displaying test cells at least.

import UIKit

class DetailViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UITableViewDelegate, UITableViewDataSource {

var imageView : UIImageView!
var tableView : UITableView!
var nameTextField : UITextField!
var descriptionTextField : UITextField!
var saveButton : UIButton!
var chooseImageButton : UIButton!
var imagePicker : UIImagePickerController?
var chosenImage : UIImage?
var recipe : Recipe?

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.whiteColor()

    if let recipe = self.recipe {

    self.updateWithRecipe(recipe)

    }

    self.setUpTableView()
    self.setUpImagePicker()
    self.setUpSubviews()

}

func updateWithRecipe(recipe: Recipe) {



}

func setUpTableView() {

    tableView = UITableView.new()
    tableView.dataSource = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    tableView = UITableView(frame: CGRectMake(self.view.frame.size.width / 10, self.view.frame.size.height / 2, self.view.frame.size.width - self.view.frame.size.width / 5, self.view.frame.size.height / 2 - 20), style: UITableViewStyle.Grouped)
    tableView.layer.cornerRadius = 10
    tableView.layer.borderColor = UIColor.blackColor().CGColor
    tableView.layer.borderWidth = 2
    self.view.addSubview(tableView)

}


func setUpImagePicker() {



}

func setUpSubviews() {



}


//table view data source

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 3
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell



    cell.textLabel?.text = "test"
    cell.textLabel?.numberOfLines = 0

    return cell

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func saveRecipeData() {



}

func saveIngredient() {



}

回答1:


You created a new table view after your assigned properties of the existing table view.

func setUpTableView() {

    // First new table view is created.
    tableView = UITableView.new()
    tableView.dataSource = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    // Second new table view is created. Everything set before this is wiped away.
    tableView = UITableView(frame: CGRectMake(self.view.frame.size.width / 10, self.view.frame.size.height / 2, self.view.frame.size.width - self.view.frame.size.width / 5, self.view.frame.size.height / 2 - 20), style: UITableViewStyle.Grouped)
    tableView.layer.cornerRadius = 10
    tableView.layer.borderColor = UIColor.blackColor().CGColor
    tableView.layer.borderWidth = 2
    self.view.addSubview(tableView)
}

Fixing this is a simple matter of creating only one table view.

func setUpTableView() {
    // Create only one table view.
    tableView = UITableView(frame: CGRectMake(self.view.frame.size.width / 10, self.view.frame.size.height / 2, self.view.frame.size.width - self.view.frame.size.width / 5, self.view.frame.size.height / 2 - 20), style: UITableViewStyle.Grouped)

    tableView.dataSource = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    tableView.layer.cornerRadius = 10
    tableView.layer.borderColor = UIColor.blackColor().CGColor
    tableView.layer.borderWidth = 2

    self.view.addSubview(tableView)
}


来源:https://stackoverflow.com/questions/31685740/uitableview-datasource-functions-not-called-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!