UITableViewCell内部的UICollectionView:在iPhone 6S Plus中崩溃
以下内容在iPhone6sPlus中运行得并不完美。 按预期在iphone5s中运行。 这只是一个示例代码。它也有同样的问题 行: 内容:5个单元格UITableViewCell内部的UICollectionView:在iPhone 6S Plus中崩溃,iphone,uitableview,swift2,uicollectionview,xcode7,Iphone,Uitableview,Swift2,Uicollectionview,Xcode7,以下内容在iPhone6sPlus中运行得并不完美。 按预期在iphone5s中运行。 这只是一个示例代码。它也有同样的问题 行: 内容:5个单元格 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var RetCell = UITableViewCell() if(indexPath.row == 0
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var RetCell = UITableViewCell()
if(indexPath.row == 0)
{
let cell = self.HomeTableView.dequeueReusableCellWithIdentifier("Banner" ,forIndexPath: indexPath)
RetCell = cell
}
else if(indexPath.row == 1)
{
let cell = self.HomeTableView.dequeueReusableCellWithIdentifier("Offer",forIndexPath: indexPath)
RetCell = cell
}
else if(indexPath.row == 3)
{
let cell = self.HomeTableView.dequeueReusableCellWithIdentifier("Category",forIndexPath: indexPath)
RetCell = cell
}
else if(indexPath.row == 2)
{
let cell = self.HomeTableView.dequeueReusableCellWithIdentifier("Products",forIndexPath: indexPath)
RetCell = cell
}
else if(indexPath.row == 4)
{
let cell = self.HomeTableView.dequeueReusableCellWithIdentifier("Recent",forIndexPath: indexPath)
RetCell = cell
}
return RetCell
}
显示单元:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
print(indexPath.row)
if(indexPath.row == 2){
guard let tableViewCell = cell as? ProductsTableViewCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}
if(indexPath.row == 4){
guard let tableViewCell = cell as? RecentViewsTableViewCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}
}
同一类的扩展:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cellColl = collectionView.dequeueReusableCellWithReuseIdentifier("ProductCollection", forIndexPath: indexPath)
return cellColl
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 5
}
在TableViewCells中:ProductStableViewCells和其他单元格在扩展名中包含以下代码
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) {
CollectionView.delegate = dataSourceDelegate
CollectionView.dataSource = dataSourceDelegate
CollectionView.tag = row
CollectionView.setContentOffset(CollectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
CollectionView.reloadData()
}
var collectionViewOffset: CGFloat {
set {
CollectionView.contentOffset.x = newValue
}
get {
return CollectionView.contentOffset.x
}
}
func setCollectionViewDataSourceDelegate(dataSourceDelegate:D,forRow row:Int){
CollectionView.delegate=dataSourceDelegate
CollectionView.dataSource=dataSourceDelegate
CollectionView.tag=行
CollectionView.setContentOffset(CollectionView.contentOffset,动画:false)//如果正在滚动,则停止集合视图。
CollectionView.reloadData()
}
var collectionViewOffset:CGFloat{
设置{
CollectionView.contentOffset.x=newValue
}
得到{
返回CollectionView.contentOffset.x
}
}
崩溃消息是什么?原因:'-[UIView collectionView:numberOfItemsInSection:]:发送到实例0x7fe98852a070的无法识别的选择器'我将首先调查传递给setCollectionViewDataSourceDelegate:
的参数。该错误表明您正在将collection view数据源设置为一个类,该类未实现必需的UICollectionViewDataSource
协议方法。但它在iPhone 5s模拟器中运行良好。在6秒以上坠毁。
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) {
CollectionView.delegate = dataSourceDelegate
CollectionView.dataSource = dataSourceDelegate
CollectionView.tag = row
CollectionView.setContentOffset(CollectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
CollectionView.reloadData()
}
var collectionViewOffset: CGFloat {
set {
CollectionView.contentOffset.x = newValue
}
get {
return CollectionView.contentOffset.x
}
}