2015-01-28 5 views
0
  • Я использую нижеследующий пользовательский UITableViewCell без файла nib.
  • У меня нет проблем при просмотре его в моем UITableViewController.
  • Моя проблема с назначением текст "name" этикетки cell.name.text = "a Name" .. отмечая назначается

Можете ли вы мне помочь?Присвоение собственности пользовательского UITableViewCell

import UIKit 

class ACMenuCell: UITableViewCell { 
    @IBOutlet var name : UILabel! 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 

     name = UILabel(frame: CGRectMake(20, 10, self.bounds.size.width , 25)) 
     name.backgroundColor = .whiteColor() 
     self.contentView.addSubview(name) 
     self.contentView.backgroundColor = .blueColor() 
    } 
    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 
    override func layoutSubviews() { 
     super.layoutSubviews() 
    } 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 
    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 
+0

Можете ли вы добавить код следующего 'метода'.? 'func tableView (tableView: UITableView !, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!' –

+0

'self.bounds.size.width', скорее всего, 0 в init. –

+0

Вероятно, дубликат [Swift: получить UITableView кадр при инициализации пользовательского UITableViewCell] (http://stackoverflow.com/questions/28143984/swift-get-uitableview-frame-when-initializing-custom-uitableviewcell/28144062#28144062) –

ответ

0

Вы можете попробовать это: Создать публичную функцию в подклассе UITableViewCell, и внутри этого funxtion установить текст в метке.

(void)initializeCell 
{ 
do whatever like setting the text for label which is a subview of tableViewCell 
} 

И от cellForRowAtIndexPAth, вызовите эту функцию объекта клетки:

// taking your code for demo 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let reuseIdentifierAC:NSString = "ACMenuCell"; var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifierAC, forIndexPath:indexPath) as ACMenuCell cell = ACMenuCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifierAC) [cell initializeCell] return cell } 

Этот подход работает для меня.

+0

Спасибо, я просто разрешаю проблему, удаляя cell = ACMenuCell (стиль: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifierAC) из метода cellForRowAtIndexPath –

+0

вы можете принять ур ответ, чтобы закрыть это. –

Смежные вопросы