我正在尝试通过使用collectionView
来模仿instagram,其中一个单元格代表提要,而另一个collectionView
则位于父级collectionView
的标题中,但是不确定这是否是正确的方法。
任何建议或建议
到目前为止,我可以显示来自父collectionview
的自定义单元格的数据,但是标题为collectionvew
的标题为空白
父母CollectionVIew
或FeedVC
import UIKit
class FeedVC: UIViewController {
var datta = ["hgfhhgh hfgh","erteeertrt","dfdfdfd","ssss"]
var allImages = [UIImage(named: "pants"), UIImage(named: "shirt"), UIImage(named: "tesla"), UIImage(named: "skull"), UIImage(named: "avatar"), UIImage(named: "heart")]
static var rever = [UIImage(named: "skull"), UIImage(named: "avatar"), UIImage(named: "heart"), UIImage(named: "hart"), UIImage(named: "heart"), UIImage(named: "tesla")]
lazy var navSearchBar = UISearchBar()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// var b = UIBarButtonItem.menuButton(self, action: #selector(test), imageName: "avatar")
// navigationItem.leftBarButtonItem = b
}
@IBOutlet weak var profileNavButtonImage: UINavigationItem!
@IBOutlet weak var searchOutlet: UIBarButtonItem!
@IBAction func SearchButtonPressed(_ sender: UIBarButtonItem) {
let sb = storyboard?.instantiateViewController(withIdentifier: "SearchVCID") as! SearchVC
present(sb, animated: true, completion: nil)
}
@objc func test(){
print("pressed")
}
@IBAction func calendarPopUp(_ sender: UIButton) {
print("uop")
let sb = storyboard?.instantiateViewController(withIdentifier: "CalendarPopUpID") as! CalendarPopUp
present(sb, animated: true, completion: nil)
}
}
extension FeedVC: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return allImages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "feedCell", for: indexPath) as! FeedCell
cell.userPost.image = FeedVC.rever[indexPath.row]
cell.profileImageOutlet.image = allImages[indexPath.row]
return cell
}
}
extension FeedVC: UINavigationBarDelegate, UISearchBarDelegate{
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
}
}
CollectionView
或FeedVC
中基本单元格的自定义单元格
import UIKit
class FeedCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
profileImageOutlet.layer.cornerRadius = profileImageOutlet.frame.width / 2
}
//MARK: Profile Image
@IBOutlet weak var profileImageOutlet: UIImageView!
//User Post Button Image
@IBOutlet weak var userPost: UIImageView!
// //Like Button Image
// @IBOutlet weak var likeButtonOutlet: UIImageView!
//
// //Comment Button Image
// @IBOutlet weak var commentButtonOutlet: UIImageView!
var stories = [UIImage(named: "skull"), UIImage(named: "avatar"), UIImage(named: "heart"), UIImage(named: "hart"), UIImage(named: "heart"), UIImage(named: "tesla")]
}
extension CollectionViewCell: UICollectionViewDataSource, UICollectionViewDelegate{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return FeedVC.rever.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "storiesCell", for: indexPath) as! StoriesCell
cell.storiesPic.image = FeedVC.rever[indexPath.row]
return cell
}
}
页眉中Collectionview
的故事单元格
import UIKit
class StoriesCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
storiesPic.layer.cornerRadius = storiesPic.frame.height / 2
}
@IBOutlet weak var storiesPic: UIImageView!
}