首页 > 解决方案 > 无法在 Swift 中的 Collectionview 中显示所有 JSON 响应值(错误:索引超出范围)

问题描述

我在tableview单元格中使用collectionview ..在这里我想在collectionview中显示tableview单元格一个值..但一直我只得到一个filename值,即使它有值数组

我在控制台中的 JSON 响应:

JSON: {
result =     {
    bids =         (
                    {
         
            "get_attachments" =                 (
                                    {
                    filename = "1627385763-6142.png";
                },
                                    {
                    filename = "1627388474-7134.jpeg";
                },
                                    {
                    filename = "1627398168-8608.jpeg";
                }
            );
            "get_bid_user" =                 {
               
                fname = "new user";
                gender = M;
            };
        },
                    {
            "get_attachments" =                 (
                                    {
                                        
                    filename = "1627387642-9066.jpg";
                    
                }
            );
            "get_bid_user" =                 {
                fname = Akash;
                gender = M;
            };
        }
    );

模型JSON

public class ViewProposalResult {
public var bids : Array<Bid>?
}

public class Bid {

public var get_attachments : Array<Get_attachments>? 
 }

public class Get_attachments {
public var filename : String?
}

在这里我想要collectionview中的get_attachments所有filename值..怎么样?

对于 tableview 和 collectionview 代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "ViewProposalTableVIewCell", for: indexPath) as! ViewProposalTableVIewCell


var bidData = viewproposalData?.result?.bids?[indexPath.row]

cell.bidAttatchment = bidData?.get_attachments

cell.attetchmentsCollectionview.reloadData()
return cell
}

//collectionview code
class ViewProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {


@IBOutlet weak var attetchmentsCollectionview: UICollectionView!

public var bidAttatchment: Array<Get_attachments>?

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return bidAttatchment?[section].filename?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BidAttatchmentCollectionViewCell", for: indexPath) as! BidAttatchmentCollectionViewCell

    var attatchBid = bidAttatchment?[indexPath.item]//getting error "Index out of range:"
    cell.attatchmentLbl.text = attatchBid?.filename
    
    
    return cell
}

错误:

致命错误:索引超出范围

我哪里错了。请指导我

标签: jsonswiftuicollectionviewcell

解决方案


首先统一变量的命名,更清楚什么是单个对象,什么是数组。

  • 替换bidAttatchmentattachments(复数形式)
  • 替换Get_attachmentsAttachment(单数形式)
  • 将数组声明为非可选的空数组。好处是你摆脱了所有的问号

然后 innumberOfItemsInSection只返回 中的项目数attachments。您的数据源不包含部分,因此该section参数无关紧要。

class ViewProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {
    
    
    @IBOutlet weak var attetchmentsCollectionview: UICollectionView!
    
    public var attachments = Array<Attachment>()
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return attachments.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BidAttatchmentCollectionViewCell", for: indexPath) as! BidAttatchmentCollectionViewCell
    
        let attatchBid = attachments[indexPath.item]
        cell.attatchmentLbl.text = attatchBid.filename
        
        return cell
    }

推荐阅读