UITableView数据源函数未调用(快速) [英] UITableView datasource functions not called (swift)

查看:88
本文介绍了UITableView数据源函数未调用(快速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我第二个工作很热,但是由于某种原因,它已经停止工作,并且表格视图只是空白。

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

我将Detail View Controller设置为UITableViewDataSource和UITableViewDelegate,并做了tableview.datasource = self和委托,但是方法没有被调用。它的构建和运行以及所有内容。

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.

谢谢!

这是我的Detail VC代码。就其他方法而言,它尚未完全完成,但表格视图至少应显示测试单元。

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() {



}


推荐答案

您在分配了现有表视图的属性之后创建了一个新的表视图。

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

这篇关于UITableView数据源函数未调用(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆