TableView中的多个CollectionView-Swift



我在TableView中使用CollectionView。已将多行添加到此CollectionView中。正如你现在在照片中看到的,已经添加了2个collectionViews。两个collectionViews中都添加了相同的数据。如何将数据分别添加到两者中?我想把它组合成一个数组,如下所示,把第一个数组的项目添加到第一个collectionView,把第二个添加到第二个collectionView。

anaSayfaViewController

extension anaSayfaViewController : UITableViewDelegate { }
extension anaSayfaViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return kategoriIsımYeni[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return kategoriIsımYeni.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow      
cell.setData(data: davetiyefilee)
cell.setDataa(collectionData: collectionData)
return cell
}
}
class anaSayfaViewController: UIViewController, UISearchBarDelegate {

var collectionData: [Int: [String]] = [:]
var davetiyefilee = [String]()
var davetiyefilee2 = [String]()
@objc func davetiyeCEK1(){
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["davetiyefilee"] as? String {
let s = String(describing: soru_baslik)
self.davetiyefilee.append(s)
self.collectionData[0] = self.davetiyefilee
DispatchQueue.main.async {                       self.tableViewKategoriler.reloadData()
} } } } }
@objc func davetiyeCEK1(){
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["davetiyefilee"] as? String {
let s = String(describing: soru_baslik)
self.davetiyefilee.append(s)
self.collectionData[0] = self.davetiyefilee2
DispatchQueue.main.async {                       self.tableViewKategoriler.reloadData()
} } } } }

UITableViewCell

class CategoryRow : UITableViewCell {
@IBOutlet weak var firstColView: UICollectionView!
var data = [String]()
var collectionData: [Int: [String]] = [:]
}
extension CategoryRow : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return  collectionData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellDav1", for: indexPath) as! anaSayfaCell
let urlNew = URL(string: collectionData[indexPath.item]![indexPath.row])
cell.denemeImage.sd_setImage(with: urlNew)

return cell
}

func setData(data: [String])
{
self.data = data
self.firstColView.reloadData()
}
func setDataa(collectionData: [Int: [String]])
{
self.collectionData = collectionData
self.firstColView.reloadData()
}
}

extension CategoryRow : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemsPerRow:CGFloat = 4
let hardCodedPadding:CGFloat = 5
let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
return CGSize(width: itemWidth, height: itemHeight)
}
}

根据数组计数设置集合视图计数

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch collectionView{
case colView1 : return dataArray1.count
case colView2 : return dataArray2.count
default : retun 0
}
}

根据收藏视图获取图片url

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellDav1", for: indexPath) as! anaSayfaCell
switch collectionView{
case colView1 :
let urlNew = URL(string: dataArray1[indexPath.row].url)
cell.denemeImage.sd_setImage(with: urlNew)
case colView2 : 
let urlNew = URL(string: dataArray2[indexPath.row].url)
cell.denemeImage.sd_setImage(with: urlNew)
default : break
}
return cell
}

最新更新