【问题标题】:Unable to create cell in collectionview?无法在集合视图中创建单元格?
【发布时间】:2017-02-10 09:43:06
【问题描述】:

我想在表格视图单元格中添加一个UICollectionView。为此,我创建了以下文件。

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
  }

}

我还创建了 CollectionView.xib

现在,由于我想为此目的在 tableview 单元格中添加一个 collectionview,我创建了一个自定义 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 上显示数据,所以我创建了 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 时将数据源发送到我的 collectionview。但是我在 collectionview 中的 cellForItemAtIndexPath 中崩溃了。我在 cellForItemAyIndexPath 中将单元格设为 nil。请告诉我有什么问题它。

【问题讨论】:

  • 你能给我发演示吗?我会解决并回复你
  • 我如何发送给你
  • 我发送请检查
  • 是的,很快就给你解决方案
  • 请检查我的回答,按照我在回答中提到的进行更改。如果它有效,请接受并支持它

标签: ios iphone swift uitableview uicollectionview


【解决方案1】:

在 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

【讨论】:

  • 感谢您的帮助,但我没有看到任何创建的 collectionview 单元格
  • @iOSGuy 只需将 self.backgroundColor = UIColor.red 写入 collectioncell,您会看到每个部分创建了 3 个不同大小的单元
  • 您需要通过收集单元格来更改单元格标识符
  • 您需要根据需要更改单元格大小所以,请接受我的回答,我解决了您的问题。
  • 为什么我需要再次用笔尖制作单元格?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多