Swift 自定义 UITableViewCell 不显示数据 [英] Swift Custom UITableViewCell not displaying data

查看:43
本文介绍了Swift 自定义 UITableViewCell 不显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 和 iOS 开发的新手.我正在尝试创建自定义 UITableViewCell.我在 UIViewController 内的 UITableView 顶部的主故事板中创建了单元格.当我加载一个默认单元格时,我能够用数据填充它.但是,现在我使用的是自定义单元格,我无法在表格中显示任何数据.我已经浏览了互联网上发布的各种教程和问题,但我无法弄清楚为什么它不起作用.任何帮助,将不胜感激.

I am new to Swift, and iOS development in general. I am attempting to create a custom UITableViewCell. I have created the cell in my main storyboard on top of a UITableView that is inside a UIViewController. When I loaded one of the default cells, I was able to populate it with data. However, now that I am using a custom cell, I cannot get any data to appear in the table. I have gone through all kinds of tutorials and questions posted on the internet, but I can't figure out why it is not working. Any help would be appreciated.

这是我为 tableview 所在的 UIViewController 编写的代码.

Here is my code for the UIViewController that the tableview resides in.

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tblView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //self.tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier : "Cell")
        self.tblView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier : "Cell")
        tblView!.delegate = self
        tblView!.dataSource = self

    }


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



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataMgr.data.count
    }


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

        let cell : CustomTableViewCell = self.tblView.dequeueReusableCellWithIdentifier("Cell", forIndexPath : indexPath) as! CustomTableViewCell

        var values = dataMgr.data[indexPath.row]
        cell.newTotalLabel?.text = "\(values.newTotal)"
        cell.winLoseValueLabel?.text = "\(values.newTotal - values.currentTotal)"
        cell.dateLabel?.text = "5/17/2015"


        return cell
    }
}

我已经逐步完成了为单元格变量赋值的程序.变量值"正在填充数据,但是当跨过单元格变量的赋值线时,我发现它们从未被赋值.它们都保持为零.

I have stepped through the program where it is assigning values to the cell variables. The variable 'values' is being populated with data, but when stepping over the assignment lines to the cell variables, I found that they are never assigned. They all remain nil.

推荐答案

在故事板中创建自定义单元格时,不要注册该类(或其他任何内容).只需确保在故事板中为单元格提供与您传递给 dequeueReusableCellWithIdentifier:forIndexPath: 相同的标识符.

When you make a custom cell in the storyboard, don't register the class (or anything else). Just be sure to give the cell the same identifier in the storyboard that you pass to dequeueReusableCellWithIdentifier:forIndexPath:.

这篇关于Swift 自定义 UITableViewCell 不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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