【发布时间】:2018-12-31 05:56:08
【问题描述】:
我在UITableView 中有viewForHeaderInSection 基于viewForHeaderInSection 数据每个UITableviewCell 在UICollectionViewCell 中有UICollectionView 我必须显示“产品”和“数量”的数据每个“产品”的数量每个viewForHeaderInSection 的“数量”都不同。我能够实现 tableview 标题和单元格数据,但我无法基于 json “order_id”、“order_unique_id”、“store_name”和“otp_store”中的 UITableViewHeader 数据获取 UICollectionViewCell 数据是 tableview 标题部分数据而“user_details”是tableviewcelluicollectionviewcell“产品”和“数量”的数据
{
"status": "1",
"error": false,
"data": [
{
"order_id": "11",
"order_unique_id": "ORDR-1001",
"store_name": "24X7",
"otp_store": "781103",
"product": [
"Product One",
"Product Two"
],
"qty": [
"1",
"3"
],
"user_details": {
"name": "Pankaj Ravi",
"number": "0999889",
"landmark": "PBH",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Home"
},
"status": "2",
"date": "2018-12-13",
"time": "14:37:57"
},
{
"order_id": "1",
"order_unique_id": "ORDR-988",
"store_name": "24X7",
"otp_store": "957505",
"product": [
"Product Eight"
],
"qty": [
"1"
],
"user_details": {
"name": “Jhon”,
"number": “999996",
"landmark": “Ithum",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Office"
},
"status": "0",
"date": "2018-11-24",
"time": "12:41:02"
}
]
}
UITableview 代码
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print(section)
let headerCell = Bundle.main.loadNibNamed("CustomHeaderCell", owner: self, options: nil)?.first as! CustomHeaderCell
if runnerArray.count > 0 {
headerCell.btnHeader.setTitle("\("x " + runnerArray[section].order_unique_id)", for: UIControl.State.normal)
var status = runnerArray[section].status
if status == "0" {
status = "Pending"
} else if status == "1" {
status = "Dispatch"
} else {
status = "Complete"
}
let lblHeaderText = status + " " + runnerArray[section].time + " , " + runnerArray[section].date
headerCell.lblHeader.text = lblHeaderText
return headerCell
} else {
return UIView()
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func numberOfSections(in tableView: UITableView) -> Int {
return runnerArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as? CustomTableViewCell {
cell.setDataForOTPStoreAndName(runner: runnerArray[indexPath.section])
cell.setDataForProduct(userDetails: userDetailsArray[indexPath.section])
cell.selectionStyle = .none
return cell
}
return UITableViewCell()
}
UICollectionView 单元的代码
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Note:- Here i am not able to find how to return array count based on UItableviewheadersection data
return productArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductsCollectionViewCell", for: indexPath) as? ProductsCollectionViewCell {
cell.lblProductName.text = productArray[indexPath.row]
return cell
}
return UICollectionViewCell()
}
【问题讨论】:
-
请通过您的
tableview和collectionview委托和数据源方法的完整代码 -
@PratikPrajapati 添加了 UITableview 和 UICollectionview 的代码
-
在 tableView
cellForRowAt方法中,您必须传递您的集合数据,例如cell.productArray = runnerArray[indexPath.section].product和cell.productArray = runnerArray[indexPath.section].qty然后在cell.collectionView.reloadData()中重新加载您的集合视图 -
@PratikPrajapati 非常感谢它对我有用
标签: uicollectionview swift4 uicollectionviewcell uitableviewsectionheader