CustomUITableView 中的标签文本没有改变
Text of label in CustomUITableView is not changing
我使用下面的代码更改自定义标签的文本 uitableviewcell
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//NSArray *allsubviews = [[NSBundle mainBundle] loadNibNamed:@"LICustomUI" owner:nil options:nil];
//LIMerchantTableViewCell *cell = [allsubviews objectAtIndex:LIApplicationLayout() == LIApplicationDirectionRightToLeft ? 8 : 9];
let allSubViews = NSBundle.mainBundle().loadNibNamed("LICustomUI", owner: nil, options: nil)
var cell:LIInsuranceNameTableViewCell = allSubViews[11] as! LIInsuranceNameTableViewCell
cell.insuranceName.text = "Hello"
println(cell.insuranceName)
cell.insuranceName.backgroundColor = UIColor.redColor();
// cell.backgroundColor = UIColor.redColor();
return cell
}
println()
日志的结果是:
<UILabel: 0x7ad71930; frame = (0 11; 320 21); text = 'Hello'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ad71a10>>
但在设备(模拟器)中文本没有改变!
如果您使用的是故事板,请确保已将标签连接到 insuranceName
出口(如果没有,只需按住 Control 键从标签拖动到您在助理检查器中找到的出口).
此外,使用 println(cell.insuranceName.text)
,而不仅仅是 println(cell.insuranceName)
。
从下面重构您的代码。
1.在viewDidLoad中注册nib
2. cellForRowAtIndexPath
中的双端队列
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerNib( UINib(nibName:"nibName", bundle: nil), forCellReuseIdentifier: "identifier");
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myTableCell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath) as! UITableViewCell;
let subViews = myTableCell.contentView.subviews as! Array
if subViews.count > 0 {
if let myLabel = subViews[0] as! UILabel {
myLabel.text = "All good to go";
}
}
}
我使用下面的代码更改自定义标签的文本 uitableviewcell
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//NSArray *allsubviews = [[NSBundle mainBundle] loadNibNamed:@"LICustomUI" owner:nil options:nil];
//LIMerchantTableViewCell *cell = [allsubviews objectAtIndex:LIApplicationLayout() == LIApplicationDirectionRightToLeft ? 8 : 9];
let allSubViews = NSBundle.mainBundle().loadNibNamed("LICustomUI", owner: nil, options: nil)
var cell:LIInsuranceNameTableViewCell = allSubViews[11] as! LIInsuranceNameTableViewCell
cell.insuranceName.text = "Hello"
println(cell.insuranceName)
cell.insuranceName.backgroundColor = UIColor.redColor();
// cell.backgroundColor = UIColor.redColor();
return cell
}
println()
日志的结果是:
<UILabel: 0x7ad71930; frame = (0 11; 320 21); text = 'Hello'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ad71a10>>
但在设备(模拟器)中文本没有改变!
如果您使用的是故事板,请确保已将标签连接到 insuranceName
出口(如果没有,只需按住 Control 键从标签拖动到您在助理检查器中找到的出口).
此外,使用 println(cell.insuranceName.text)
,而不仅仅是 println(cell.insuranceName)
。
从下面重构您的代码。 1.在viewDidLoad中注册nib 2. cellForRowAtIndexPath
中的双端队列override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerNib( UINib(nibName:"nibName", bundle: nil), forCellReuseIdentifier: "identifier");
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myTableCell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath) as! UITableViewCell;
let subViews = myTableCell.contentView.subviews as! Array
if subViews.count > 0 {
if let myLabel = subViews[0] as! UILabel {
myLabel.text = "All good to go";
}
}
}