2015-01-09 4 views
0

작동하지 않습니다있는 UITableViewCell을 추가스위프트 프로그래밍이 코드가 제대로

class Cell:UITableViewCell { 
    var fullName:UILabel! = UILabel() 
    var userImage:UIImage! = UIImage() 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     println("wake") 
     fullName.setTranslatesAutoresizingMaskIntoConstraints(false) 
     contentView.addSubview(fullName) 
     let imageView = UIImageView(image: userImage) 
     contentView.addSubview(imageView) 
     var viewsDict = [ 
      "fullName" : fullName, 
      "userImage" : userImage 
     ] 

     contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[userImage(10)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict)) 
     contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[fullName]-[userImage(10)]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict)) 
    } 
} 

의 viewDidLoad :

var tableView = UITableView() 
tableView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height-64) 
tableView.delegate = self 
tableView.dataSource = self 
var cell = Cell.self 
tableView.registerClass(cell, forCellReuseIdentifier: "cell") 
self.view.addSubview(tableView) 

cellForRowAtIndexpath :

let cell:Cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as Cell 
cell.fullName.text = testarray.objectAtIndex(indexPath.row) as? String 
return cell 

내가 레이블을 볼 수 없습니다 셀에서 심지어 코드를 실행할 때 심지어 println ("wake") 클래스 셀에서 실행되지 않습니다.

+0

프로그래밍 방식으로 셀을 생성한다고 말했을 때. 그러면 'awakeFromNib()'메소드가 그림에 포함되지 않습니다. – Kampai

답변

0

사용자 정의 UITableViewCell 코드 아래와 같이해야 제공된 nib 파일에서 셀 객체로드 :

class ListTableViewCell: UITableViewCell { 

    var lblTitle = UILabel() 

    override init(style: UITableViewCellStyle, reuseIdentifier: String!) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     lblTitle = UILabel(frame: CGRectZero) 
     lblTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18.0) 
     lblTitle.textAlignment = NSTextAlignment.Right 
     lblTitle.numberOfLines = 1 
     lblTitle.backgroundColor = UIColor.clearColor() 
     lblTitle.textColor = UIColor(red: 130/255, green: 137/255, blue: 141/255, alpha: 1.0) 
     self.contentView.addSubview(self.lblTitle) 
     // 130 137 141 
     self.separatorInset = UIEdgeInsets(top: 0, left: 100, bottom: 0, right: -100) 

    } 

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

    override func layoutSubviews() { 

     var marginX = CGFloat(30.0) 
     var marginY = CGFloat(0.0) 
     var width = CGFloat(320) 
     var height = self.contentView.frame.size.height 

     self.lblTitle.frame = CGRectMake(marginX, marginY, width, height) 
    } 

} 

을 그리고 이것은 당신이 cellForRowAtIndexPath 방법으로 세포를 만들 수있는 방법이다 UITableView 데이터 소스

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell:ListTableViewCell! = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) as? ListTableViewCell 

     if (cell == nil) { 
      cell = ListTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: self.cellIdentifier) 
     } 
     cell.lblTitle.text = self.listItems[indexPath.row] 
     return cell 
    } 
+0

답변의 두 번째 부분은 필요하지 않습니다. 질문에서 대기열 해제에 아무 문제가 없습니다. – jrturton

+0

나는 완전한 코드를 제공 했으므로 나중에 다른 사람들에게도 도움이됩니다. – Kampai

0

awakeFromNib은 펜촉이나 스토리 보드에서 개체를로드 할 때만 호출됩니다. 클래스가 등록되어 있기 때문에 당신은 그렇게하지 않습니다.

이 지정된 식별자 및 새로운 셀을위한 클래스를 등록하는 경우가 생성되어야하며,이 방법 : 디큐 방법에 대한 jQuery과 문서에 언급 된

당신은 initWithStyle:reuseIdentifier 방법에 그 코드를 이동해야 initWithStyle:reuseIdentifier: 메서드를 호출하여 셀을 초기화합니다. 펜촉 기반 세포의 경우,이 방법은

관련 문제