2014-11-03 116 views
0

我有我正在设计的自定义单元格。自定义单元格标签

enter image description here

我想传递一个字符串,该自定义单元格。我做了所谓的AlarmCell(UITableCell的子类)可可触摸类,然后用这个代码(在我的UIViewController类),把它称为:

let myCell = AlarmCell() 

然后从内AlarmCell叫的UILabel。

myCell.timeLabel?.text = newAlarm 

newAlarm具有字符串 “下午8时38”

怎么会myCell.timelabel是走出来的零?我究竟做错了什么?

编辑:

AlarmCell类:

class AlarmCell: UITableViewCell { 
    @IBOutlet var timeLabel: UILabel! 
} 

报警(UIViewController类):

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let Cell = tableView.dequeueReusableCellWithIdentifier("alarmCell", forIndexPath: indexPath) as UITableViewCell 

我:

cell.textLabel?.text = alarms[indexPath.row] 

但我需要传递一个字符串到一个客户细胞内的汤姆LABL

+0

你可以采取与CMD + SHIFT + 3 – 2014-11-03 02:51:14

+0

连接是否正确网点截图?我们可以看到更多的'AlarmCell'类的代码和单元格的加载位置('tableView:cellForRowAtIndexPath:')吗? – 2014-11-03 02:53:55

+0

IBOutlet字体是否有空圈? – Tinyfool 2014-11-03 03:15:13

回答

1

请你帮个忙,并创建您的AlarmCell相关的笔尖文件.swift类。在自定义nib文件内部的单元之后,在您的viewDidLoad函数中声明以下内容。

var nib = UINib(nibName: "AlarmCell", bundle: nil) 
tableView.registerNib(nib, forCellReuseIdentifier: "cell") 

然后,在你的cellForRowAtIndexPath功能提到它像这样:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell:AlarmCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as AlarmCell 
    cell.timeLabel.text = alarms[indexPath.row] 
    return cell 
} 
0

尝试改变cell.textLabel?.text = alarms[indexPath.row]

Cell.timeLabel?.text = NSString(format: "%@", alarms[indexPath.row])

你应该如下更改用于声明内func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell方法Cell对象let关键字:

var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("alarmCell") as UITableViewCell alarmCell 
0

定义单元格as

var cell: AlarmCell = self.tableView.dequeueReusableCellWithIdentifier("alarmCell") as UITableViewCell AlarmCell 

然后setTextLabel(或您的自定义标签)文本

cell.textLabel?.text = alarms[indexPath.row] as NSString 

评论下面,如果有任何疑问,因为这对我来说完美的工作