Reputation: 3459
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() {
}
Upvotes: 1
Views: 2667
Reputation: 42598
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)
}
Upvotes: 3