【问题标题】:Swift TableView cell doesnt showSwift TableView 单元格不显示
【发布时间】:2017-08-29 11:13:23
【问题描述】:

我的 TableViewController 没有显示单元格内容的问题。我有一个按钮可以加载有效但也应该显示在单元格中的数据。我是否忘记了“vieDidLoad”函数中的某些内容?您可以在最后找到 Storyboard 和应用程序中的图像。

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UINib(nibName: "TableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "TableViewCell")
}
override func viewWillAppear(_ animated: Bool) {
    let btn = UIButton(frame: CGRect(x: 0, y: 25, width: self.view.frame.width, height: 50))
    btn.backgroundColor = UIColor.cyan
    //btn.setTitle("Add new Dummy", for: UIControlState)
    btn.addTarget(self, action: #selector(addDummyData), for: UIControlEvents.touchUpInside)
    self.view.addSubview(btn)
}
@objc func addDummyData() {
    RestApiManager.sharedInstance.getRandomCar { (json: NSArray?) in

        if let results = json {
            for entry in results {
                self.items.append(CarObject(json: entry as! Dictionary))
            }
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }
}
func tableView(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "TableViewCell") as! TableViewCell
    }
    let car = self.items[indexPath.row]
    print(car.pictureURL)
    if let url = NSURL(string: car.pictureURL) {
        if let data = NSData(contentsOf: url as URL) {
            cell.carImage?.image = UIImage(data: data as Data)
        }
    }

    // Create Swift / Cocoa Touch file with carImage etc. Actionbutton
    cell.carTitleLabel.text = "test"//car1
    cell.carPriceLabel.text = car.carPrice
    print("test3")
    return cell
}

}

Storyboard

app

【问题讨论】:

  • 您添加 numberOfRowsInSection 委托了吗?
  • TableView not reloading的可能重复
  • 我试过这些: func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.items.count; }
  • 我正在使用 UITableViewController 所以它说声明数据源和委托是多余的。我试过了。

标签: swift xcode uitableview


【解决方案1】:

就像 Rishabh 说的,你需要在 viewDidLoad 中设置数据源和委托

override func viewDidLoad {
   super.viewDidLoad()
   tableView.datasource = self
   tableView.delegate = self
}

【讨论】:

  • 对不起,我忘了在代码 sn-p 中输入“class TableViewController: UITableViewController”这一行。所以这变得多余了。
【解决方案2】:

您需要在 viewDiDLoad() 中设置 DataSource 和 Delegate 如下所示。

 self.tableView.dataSource = dataSource!

【讨论】:

    【解决方案3】:

    对于默认的UITableViewController,您应该拥有这些功能。也许你错过了其中之一。

    import UIKit
    
    class YourViewController: UITableViewController {
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1 //Your section count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count //Your row count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "TableViewCell") as! TableViewCell
        }
        let car = self.items[indexPath.row]
        print(car.pictureURL)
        if let url = NSURL(string: car.pictureURL) {
            if let data = NSData(contentsOf: url as URL) {
                cell.carImage?.image = UIImage(data: data as Data)
            }
        }
    
        // Create Swift / Cocoa Touch file with carImage etc. Actionbutton
        cell.carTitleLabel.text = "test"//car1
        cell.carPriceLabel.text = car.carPrice
        print("test3")
        return cell
    }
    
    }
    

    注意:您还应该在numberOfRowsInSection 中检查您的self.items.count。您的 return 值不应为 0。

    【讨论】:

    • 谢谢,我刚试过这个。它可能已解决,但我收到另一条错误消息:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在捆绑包中加载 NIB:'NSBundle /flowinger/Library/Developer/CoreSimulator/Devices/ 01CA5F70-F8B5-48B6-8352-1B2236F78C49/data/Containers/Bundle/...>(已加载)',名称为'TableViewCell''
    • 您是否在 Storyboard 中设置了 TableViewCell 的标识符?
    • 您应该打开 Storyboard,选择您的 TableViewCell,在右侧菜单中打开 Attributes Inspector,找到 Table View Cell 部分并在 Identifier 文本字段中添加“TableViewCell”。
    • 如果您在 xcode 之外重命名了 xib 或 storyboard 文件,可能会发生这种情况。您可以在以下链接中查看可能的修复方法:stackoverflow.com/questions/27842740/…stackoverflow.com/questions/12722461/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多