iOSGuy
iOSGuy

Reputation: 181

Unable to create cell in collectionview?

I want to add a UICollectionView inside a table view cell.For this purpose, i have created below files.

CollectionCell:

class CollectionCell: UICollectionViewCell {

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

}

I have also created a 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
  }

}

I have also created CollectionView.xib

Now as i want to add a collectionview inside a tableview cell for that purpose i have created a cutom tableview cell class as 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)

  }

}

Now i have created a mainViewController to show the data on tableview so i have 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)


  }
}

Now i am trying to send datasource to my collectionview when tableviewcell is just created.But i am getting crash in cellForItemAtIndexPath in collectionview.I am getting cell as nil in cellForItemAyIndexPath.Please tell me what is the issue with it.

Upvotes: 2

Views: 1167

Answers (1)

Jitendra Modi
Jitendra Modi

Reputation: 2394

In Collectionview.swift change this function

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

    collectionview.delegate = self
    collectionview.dataSource = self
  }

In cellforItemat change this line

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

to

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

Upvotes: 2

Related Questions