2016-08-23 5 views
0

iOS에서 UITableView를 사용하여 목록을 만들려고하지만 사용자 정의 셀을 사용할 때 항상 비어 있으며 설정된 빈 셀이 아니라 빈 셀을 표시합니다.사용자 정의 UITableViewCell을 사용할 때 iOS UITableView가 비어 있습니다.

ViewController.swift

import UIKit 

class ViewController: UITableViewController { 

    var collections = ["Collection 1", "Collection 2", "Collection 3", "Collection 4", "Collection 5", "Collection 6"] 
    var descriptions = ["Collection description 1", "Collection description 2", "Collection description 3", "Collection description 4", "Collection description 5", "Collection description 6"] 
    let cellIdentifier = "CollTableViewCell" 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let tableNib = UINib(nibName:"CollectionTableViewCell", bundle:nil) 
     tableView.registerClass(CollectionTableViewCell.self, forCellReuseIdentifier: cellIdentifier) 
     tableView.registerNib(tableNib,forCellReuseIdentifier:cellIdentifier) 
    } 

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

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 6 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionTableViewCell 
     cell.collectionName?.text = collections[indexPath.row] 
     return cell 
    } 

} 

CollectionTableViewCell.swift는

import UIKit 

class CollectionTableViewCell: UITableViewCell { 

    @IBOutlet weak var collectionName: UILabel! 
    @IBOutlet weak var collectionDesc: UILabel! 
    @IBOutlet weak var collectionImage: UIImageView! 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
    } 

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

    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

StoryBoard를 사용하고 있습니까? 그렇다면, 어떻게 생겼습니까? – ZGski

+0

IBOutlets (특히 collectionName)을 올바르게 설정 했습니까? – firstinq

+1

클래스와 펜촉을 모두 등록하지 말고 펜촉을 등록하십시오. – deadbeef

답변

0
class ViewController: UIViewController { 

    @IBOutlet weak var tblView: UITableView! 
    var collections:NSArray = [] 
    var descriptions:NSArray = [] 

    override func viewDidLoad() { 
     collections = ["Collection 1", "Collection 2", "Collection 3", "Collection 4", "Collection 5", "Collection 6"] 
     descriptions = ["Collection description 1", "Collection description 2", "Collection description 3", "Collection description 4", "Collection description 5", "Collection description 6"] 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return collections.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell 
     cell.lbl_Name.text = collections.objectAtIndex(indexPath.row) as? String 
     cell.lbl_Desc.text = descriptions.objectAtIndex(indexPath.row) as? String 
     return cell 
    } 



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


} 


class TableViewCell: UITableViewCell { 

    @IBOutlet weak var lbl_Name: UILabel! 
    @IBOutlet weak var lbl_Desc: UILabel! 

    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 
    } 

} 
관련 문제