我在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)
}
}
答案 0 :(得分:0)
根据数组计数设置集合视图计数
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch collectionView{
case colView1 : return dataArray1.count
case colView2 : return dataArray2.count
default : retun 0
}
}
根据收藏夹视图获取图像网址
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
}