无法在 Swift 的 Collectionview(错误:索引超出范围)中显示所有 JSON 响应值 [英] Unable to show all JSON response values in Collectionview(ERROR: Index out of range) in Swift
问题描述
我在tableview单元格中使用collectionview..在这里我想在collectionview中向tableview单元格显示一个值..但我一直只得到一个filename
值,即使它有一组值
I am using collectionview in tableview cell.. here i want to show tableview cells one value in collectionview.. but all the time i am getting only one filename
value even it has array of values
我在控制台中的 JSON 响应:
my JSON response in console:
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?
}
这里我想要 get_attachments
所有 filename
值在 collectionview .. 怎么样?
here i want get_attachments
all filename
values in collectionview.. how?
对于 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
}
错误:
致命错误:索引超出范围
Fatal error: Index out of range
我哪里错了.请指导我
推荐答案
首先统一变量的命名,明确什么是单个对象,什么是数组.
First of all consolidate the naming of the variables to make clearer what is a single object and what is an array.
- 将
bidAttatchment
替换为attachments
(复数形式) - 将
Get_attachments
替换为Attachment
(单数形式) - 将数组声明为非可选的空数组.好处是你摆脱了所有的问号
- Replace
bidAttatchment
withattachments
(plural form) - Replace
Get_attachments
withAttachment
(singular form) - Declare the array as non-optional empty array. The benefit is you get rid of all the question marks
然后在 numberOfItemsInSection
中返回 attachments
中的项目数.您的数据源不包含部分,因此 section
参数无关紧要.
Then in numberOfItemsInSection
just return the number of items in attachments
. Your data source doesn't contain sections so the section
parameter is irrelevant.
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
}
这篇关于无法在 Swift 的 Collectionview(错误:索引超出范围)中显示所有 JSON 响应值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!