ios - 快速自定义 uitextviewcell 标签始终为零

标签 ios uitableview swift null

我从两天前就被困在这里了,找不到如何管理这个.. 我有一个 uitableview,带有一系列自定义单元格和部分,这是我想要做的:

  • 第 1 部分:只有一行,里面有一个标签
  • 第 2 部分:日期选择器(为此我使用了 DVDatePickerTableViewCell 类)

这是表格 View 的代码

import UIKit

class DettagliRichiestaTVC: UITableViewController {
    //sections contiene le sezioni
    let sections: NSArray = ["Stato", "Data", "Priorità", "Richiesta", "Risposta"]
    //cells contiene tutte le righe della tabella, un 2D array
    var cells:NSArray = []
    var stato:String = "Completato"
    @IBOutlet weak var statoLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
       // statoLabel.text = stato

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 44

        // Cells is a 2D array containing sections and rows.
        var cellStato = cellDettagli(style: UITableViewCellStyle.Default, reuseIdentifier: "cellStato")
        cellStato.label?.text = "Ciao"
        cells = [
            [cellStato],
            [DVDatePickerTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)]
        ]

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

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

    @IBAction func selectedStato(segue:UIStoryboardSegue) {
        let statoRichiesteTVC = segue.sourceViewController as StatoRichiesteTVC
        if let selectedStato = statoRichiesteTVC.selectedStato {
            statoLabel.text = selectedStato
            stato = selectedStato
        }
        self.navigationController?.popViewControllerAnimated(true)
    }

    // MARK: - Table view data source


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerFrame:CGRect = tableView.frame


        var title = UILabel(frame: CGRectMake(10, 10, 100, 20))
        title.font = UIFont.boldSystemFontOfSize(12.0)
        title.text = self.sections.objectAtIndex(section) as? String
        title.textColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)

        var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height))
        headerView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.8)
        headerView.addSubview(title)

        return headerView
    }



    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
        if (cell.isKindOfClass(DVDatePickerTableViewCell)) {
            return (cell as DVDatePickerTableViewCell).datePickerHeight()
        }
        return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return cells.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cells[section].count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return cells[indexPath.section][indexPath.row] as UITableViewCell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
        if (cell.isKindOfClass(DVDatePickerTableViewCell)) {
            var datePickerTableViewCell = cell as DVDatePickerTableViewCell
            datePickerTableViewCell.selectedInTableView(tableView)
        }
        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        //println(segue.identifier)
        if segue.identifier == "SavePlayerDetail" {
        }
        if segue.identifier == "SelezionaStatoRichiesta" {
            let statoRichiesteTVC = segue.destinationViewController as StatoRichiesteTVC
            statoRichiesteTVC.selectedStato = stato
        }
    }

}

这是自定义单元类

import UIKit

class cellDettagli: UITableViewCell {
    @IBOutlet weak var label: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func loadItem(#Label: String) {
        label.text = Label
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }


    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

}

如果我设置 cellStato.label?.text = "Ciao",它会崩溃并显示“ fatal error :在展开可选值时意外发现 nil”。

我还创建了 .xib 文件并将其分配给 cellDettagli 类。 我总是遇到这个错误。

如何设置这个标签的值,以及日期选择器行的日期?

谢谢

最佳答案

我用这个让它工作:

var cell:cellDettagli? = tableView.dequeueReusableCellWithIdentifier("cellDettagli") as? cellDettagli
if  (cell==nil){
   var nib:NSArray=NSBundle.mainBundle().loadNibNamed("cellDettagli", owner: self, options: nil)
   cell = nib.objectAtIndex(0) as? cellDettagli
}

在我的 cellForRowAtIndexPath 中。

谢谢亚历山大的帮助!我已经在使用静态单元格和 Storyboard...!

关于ios - 快速自定义 uitextviewcell 标签始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28253048/

相关文章:

iOS 分析——假设 "self"非空

ios - 如何实现 iOS 邮件应用程序的多选行为?

iOS:如何访问 UISegmentedControl 中的各个段?

ios - 当 applicationWillTerminate 时快速启动应用程序

javascript - Iframe 背景视频不适用于某些 Iphone

ios - UITableViewCell 崩溃

ios - 在另一行删除动画之后插入行

json - Swift 从 JSON 请求生成通用函数

ios - 在 Swift 中获取联系人姓名和电话号码

objective-c - 哪种方式存储数据(图像)? NSData、字符串或可转换