嗨,我在表格单元格中创建了一个具有相同要求的 collectionView,您可以检查我的代码。
//
// ViewController.swift
// DemoApp
//
// Created by Mahesh Kumar on 09/01/18.
// Copyright © 2018 Mahesh Kumar. All rights reserved.
import UIKit
class TableCell : UITableViewCell{
@IBOutlet weak var collVw: UICollectionView!
@IBOutlet weak var categoryName: UILabel!
}
class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource , UICollectionViewDelegateFlowLayout , UITableViewDelegate,UITableViewDataSource {
var categories_array = ["Home","Helth","New","Home1","Home2","Home3","Home4","Home5","Home6","Home7","Home8","Home9","Home11","Home12","Home13","Home14","Home15"]
//Mark
var sectionArray = NSMutableArray()
@IBOutlet weak var tableVw: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Mark
var sectionCount = 0
var mainIndex = 0
let section = categories_array.count % 4
if(section == 0){
sectionCount = categories_array.count/4
}
else{
sectionCount = categories_array.count/4 + 1
}
//Mark
for _ in 0...sectionCount {
let rowsData = NSMutableArray()
var j = 0
while j<4{
if(mainIndex == categories_array.count){
break
}
rowsData.add(categories_array[mainIndex])
j = j + 1
mainIndex = mainIndex + 1
}
sectionArray.add(rowsData)
}
tableVw.reloadData()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableVw.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as? TableCell
if(indexPath.row == 1){
cell?.categoryName.text = "Top Redeemed"
}
else if(indexPath.row == 2){
cell?.categoryName.text = "Categories"
}
cell?.collVw.tag = indexPath.row
cell?.collVw.delegate = self
cell?.collVw.dataSource = self
cell?.collVw.reloadData()
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
if(collectionView.tag == 0){
return 1
}
else if(collectionView.tag == 1){
if(categories_array.count > 4){
if(categories_array.count % 4 == 0){
return categories_array.count/4
}
else{
return (categories_array.count/4) + 1
}
}
else{
return 1
}
}
else{
return 10
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = UIColor.green
if let lbl = cell.viewWithTag(1) as? UILabel{
lbl.text = "\(indexPath.row)"
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
if(collectionView.tag == 0){
return CGSize.init(width: collectionView.frame.width - 10, height: collectionView.frame.height)
}
else if(collectionView.tag == 1){
return CGSize.init(width: (collectionView.frame.width)/2 - 5.5, height: collectionView.frame.height/2 - 0.5)
}
else {
return CGSize.init(width: collectionView.frame.width/3 - 4, height: collectionView.frame.height/2 - 0.5)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
if(collectionView.tag == 0){
return 10
}
else{
return 1
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(collectionView.tag == 0){
return 10
}
else if(collectionView.tag == 1){
return ((sectionArray[section] as? NSMutableArray)?.count)!
}
else{
return 6
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("coll vw",indexPath.row)
print(indexPath.section)
//Mark
if(collectionView.tag == 1){
print(collectionView.tag)
let array = sectionArray[indexPath.section] as? NSMutableArray
print(array![indexPath.row])
}
}
}