2017-02-10 122 views
2

我想在表格視圖單元格內添加一個UICollectionView。爲此,我創建了以下文件。無法在collectionview中創建單元格?

CollectionCell:

class CollectionCell: UICollectionViewCell { 

    @IBOutlet weak var lblName: UILabel! 
    @IBOutlet weak var imgCell: UIImageView! 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

} 

我還創建了一個CollectionCell.xib

CollectionView.swift

class CollectionView: UIView { 

    @IBOutlet weak var collectionview: UICollectionView! 
    var arrItems:[String] = [] 
    /* 
    // Only override draw() if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func draw(_ rect: CGRect) { 
     // Drawing code 
    } 
    */ 
    func configureCollectionView() 
    { 
     collectionview.register(CollectionCell.self, forCellWithReuseIdentifier: "CollectionCell") 
     collectionview.delegate = self 
     collectionview.dataSource = self 
    } 
    func setCollectionViewDatasource(arrItems:[String]) 
    { 
    self.arrItems = arrItems 
    self.collectionview.reloadData() 
    } 
    static func initiateView()->CollectionView 
    { 
    let nib = UINib(nibName: "CollectionView", bundle: nil) 
    let view = nib.instantiate(withOwner: nil, options: nil)[0] as! CollectionView 
    return view 
    } 

} 

extension CollectionView:UICollectionViewDelegate 
{ 

} 
extension CollectionView:UICollectionViewDataSource 
{ 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 

    return 1 

    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return arrItems.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell:CollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell 
    cell.lblName.text = arrItems[indexPath.row] 
    return cell 
    } 

} 

我也創建收藏View.xib

現在我想添加爲目的,我創建了一個對自定義的tableview細胞類爲CustomTableCell

class CustomTableCell: UITableViewCell { 

    var view = CollectionView() 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 
    print("ceonstatructir init") 
    // insitate a nib 
    view = CollectionView.initiateView() 
    view.configureCollectionView() 
    contentView.addSubview(view) 
    view.setCollectionViewDatasource(arrItems: ["firest","second","thierd"]) 

    } 

    required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
    } 
    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 
    func setDataSource(arrItems:[String]) 
    { 
    //view.setCollectionViewDatasource(arrItems: arrItems) 

    } 

} 

我現在已經創建了一個mainViewController表現出的tableview細胞內的CollectionView上的tableview數據,所以我必須crteated viewcontroller.swift

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

    @IBOutlet weak var tableview: UITableView! 
    let sectionArray = ["First","Second","Third","Fourth","Fifth"] 
    let collectionArray = [["First","Second","Third"],["First","Second","Third"],["First","Second","Third"],["First","Second","Third"],["First","Second","Third"]] 
    override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    tableview.register(CustomTableCell.self, forCellReuseIdentifier: "TableCell") 
    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 
    func numberOfSections(in tableView: UITableView) -> Int { 

    return sectionArray.count 
    } 
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    return sectionArray[section] 
    } 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return 1 
    } 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell:CustomTableCell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! CustomTableCell 
    return cell 
    } 
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    let cell:CustomTableCell = cell as! CustomTableCell 
    let dataSource = collectionArray[indexPath.section] 
    cell.setDataSource(arrItems:dataSource) 


    } 
} 

我現在想發送的數據源到我的科爾當tableviewcell剛創建時,ectionview。但我碰到了collectionview.I中的cellForItemAtIndexPath崩潰。我在cellForItemAyIndexPath中獲取單元格爲零。請告訴我它有什麼問題。

+0

你願意給我演示?我會解決並返回給你 –

+0

我怎麼發給你 – iOSGuy

+0

我發送請檢查 – iOSGuy

回答

2

在Collectionview.swift改變這一功能

func configureCollectionView() 
    { 
    let nib = UINib(nibName: "CollectionCell", bundle: nil) 
    collectionview.register(nib, forCellWithReuseIdentifier: "cell") 

    collectionview.delegate = self 
    collectionview.dataSource = self 
    } 

在cellforItemat改變這一行

let cell:CollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell 

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionCell 
+0

感謝您的幫助,但我沒有看到任何collectionview單元格創建 – iOSGuy

+0

@iOSGuy只需寫self.backgroundColor = UIColor.red 到收集單元,你看到3個單元格創建不同大小 –

+0

您需要通過收集單元標識單元格標識符 – iOSGuy

相關問題