[英]CollectionView with images inside a TableViewCell
I want a TableView with 5 CollectionViews one by one. 我想要一个带有5个CollectionViews的TableView。 The Collection Views must display images that scroll horizontally. 集合视图必须显示水平滚动的图像。 I followed this tutorial: https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/ 我遵循了这个教程: https : //ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
But I want images scrolling in my CollectionView. 但我想在我的CollectionView中滚动图像。 I don't know how to do that. 我不知道该怎么做。
Here is my sample code: 这是我的示例代码:
import UIKit
class ViewController1: UIViewController, UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet var tableView: UITableView!
var images = [UIImage(named:"banner2"),UIImage(named:"banner1"),UIImage(named:"banner3"),UIImage(named:"banner4"),UIImage(named:"banner5")]
var storedOffsets = [Int: CGFloat]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! Cell2
cell.frame = CGRect(x: 0, y: 28, width: 375, height: 202)
cell.myCollection.reloadData()
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? Cell2 else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? Cell2 else { return }
storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
}
}
extension ViewController1: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! Cell1
cell.image.image = images[(indexPath as NSIndexPath).row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
}
}
import UIKit
class Cell1: UICollectionViewCell {
@IBOutlet var image: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
image.frame = CGRect(x: 0, y: 0, width: 175, height: 175) // Here I get exc_bad_instruction (code=exc_i386_invop subcode=0x0) warning
contentView.backgroundColor = UIColor.green
self.contentView.addSubview(image)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
import UIKit
class Cell2: UITableViewCell {
@IBOutlet var myCollection: UICollectionView!
override func awakeFromNib() {
myCollection.frame = CGRect(x: 0, y: 28, width: 375, height: 202)
}
}
extension Cell2 {
func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {
self.myCollection.delegate = dataSourceDelegate
self.myCollection.dataSource = dataSourceDelegate
myCollection.tag = row
myCollection.setContentOffset(myCollection.contentOffset, animated:false) // Stops collection view if it was scrolling.
let layout = UICollectionViewFlowLayout.init()
layout.scrollDirection = .horizontal
layout.sectionInset = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.itemSize = CGSize(width: 182, height: 182)
myCollection = UICollectionView.init(frame: myCollection.frame, collectionViewLayout: layout)
myCollection.register(Cell1.self, forCellWithReuseIdentifier: "cell1")
self.contentView.addSubview(myCollection)
myCollection.reloadData()
}
var collectionViewOffset: CGFloat {
set { myCollection.contentOffset.x = newValue }
get { return myCollection.contentOffset.x }
}
}
A pink TableView (I set the colour) and black CollectionView(Again, I set the colour) I can't view my images or my CollectionViewCells that I have set as greenColor. 粉红色的TableView(我设置了颜色)和黑色的CollectionView(再次,我设置了颜色)我无法查看我的图像或我设置为greenColor的CollectionViewCells。 And when touch in the pink part or try scroll I get the error: fatal error: unexpectedly found nil while unwrapping an Optional value with the warning - exc_bad_instruction (code=exc_i386_invop subcode=0x0) 当触摸粉红色部分或尝试滚动时我得到错误: 致命错误:在解除带警告的可选值时意外发现nil - exc_bad_instruction(code = exc_i386_invop subcode = 0x0)
Can someone tell me how I can do this? 谁能告诉我怎么做到这一点? Thanks in advance 提前致谢
I use Xcode 8 and Swift 3. 我使用Xcode 8和Swift 3。
I have snippet in OBJECTIVE- C , i think you have to convert into swift 3.0 我在OBJECTIVE-C中有片段,我认为你必须转换成swift 3.0
Here is my code 这是我的代码
You can try it 你可以试试
tablecell cellForRowAtIndexPath tablecell cellForRowAtIndexPath
static NSString *CellIdentifier = @"cell3";
CollectionViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CollectionViewTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.headerLabel.text = @"DISH TYPE";
cell.collectionView.delegate = self;
cell.collectionView.dataSource = self;
cell.collectionView.tag = indexPath.row;
[cell.collectionView reloadData];
and collection view cellForItemAtIndexPath 和集合视图cellForItemAtIndexPath
if(collectionView.tag == 4){
FIPhotoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"foodTagCell" forIndexPath:indexPath];
NSDictionary *dic = [arrAmenities objectAtIndex:indexPath.row];
NSMutableDictionary *dic1 = [selectedAmenities objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[dic valueForKey:@"ImageURL"]];
[cell.photoImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
return cell;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.