Unable to show all JSON response values in Collectionview(ERROR: Index out of range) in Swift
Unable to show all JSON response values in Collectionview(ERROR: Index out of range) in Swift
我在 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?
}
这里我想要 get_attachments
collectionview 中的所有 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
}
错误:
Fatal error: Index out of range
我哪里错了。请指导我
首先统一变量的命名,明确什么是单个对象,什么是数组。
- 将
bidAttatchment
替换为attachments
(复数形式)
- 将
Get_attachments
替换为Attachment
(单数形式)
- 将数组声明为非可选的空数组。好处是你摆脱了所有的问号
然后在 numberOfItemsInSection
中只是 return 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
}
我在 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?
}
这里我想要 get_attachments
collectionview 中的所有 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
}
错误:
Fatal error: Index out of range
我哪里错了。请指导我
首先统一变量的命名,明确什么是单个对象,什么是数组。
- 将
bidAttatchment
替换为attachments
(复数形式) - 将
Get_attachments
替换为Attachment
(单数形式) - 将数组声明为非可选的空数组。好处是你摆脱了所有的问号
然后在 numberOfItemsInSection
中只是 return 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
}