iOS开发:TableView内嵌CollectionView无法正确填充数组数据
解决TableView内嵌CollectionView的预设数组填充问题
我来帮你搞定这个数据匹配的问题!从你的代码和需求来看,核心是要把预设的tag1到tag11数组,精准对应到每个TableView Section里的CollectionView中,下面是具体的分析和解决方案:
问题根源分析
- 你已经把预设数组整理到了
dataSource中,但没有将它和TableView Section对应的数据源twoDimensionalArrayList正确绑定,反而错误地处理了JSON返回的sub_drink数据(你的需求是用预设数组,这部分代码完全多余)。 - 当前
twoDimensionalArrayList里的names字段被硬编码成了["11"],导致CollectionView根本拿不到正确的子品牌数据。 - 没有给内嵌的CollectionView设置和TableView Section关联的标识,导致数据无法正确对应。
分步解决方案
1. 修正JSON数据处理逻辑,关联预设数组
修改getDrinkDataFromJSON方法,直接把预设数组绑定到每个饮品类型对应的Section中,删除无用的sub_drink处理代码:
func getDrinkDataFromJSON(){ let urlString = "http://rednwhite.co.in/webservices/drinks.php" Alamofire.request(urlString, method: .get).responseJSON{ (response) in print(" response :\(String(describing: response.request))") guard response.result.isSuccess else { print("Block 1 ") print(" response :\(response)") print("Error with response: \(String(describing: response.result.error))") return } guard let dict = response.result.value as? Dictionary <String,AnyObject> else { print(" response :\(response)") print("Error with dictionary: \(String(describing: response.result.error))") return } guard let dictData = dict["message"] as? [Dictionary <String,AnyObject>] else { print("test 3") print("Error with dictionary data: \(String(describing: response.result.error))") return } print(" dict data\(dictData)") self.drinkTypeNameArray.removeAll() self.twoDimensionalArrayList.removeAll() // 遍历饮品类型,同时绑定对应的预设子品牌数组 for (index, data) in dictData.enumerated() { self.drinkTypeNameArray.append(data["drink_name"]! as! String) // 容错处理:防止JSON返回的类型数量超过预设数组长度 let targetSubArray = index < self.dataSource.count ? self.dataSource[index] : [] self.twoDimensionalArrayList.append(ExpandableDrinks(isExpandable: false, names: targetSubArray)) } self.tableView.reloadData() } }
2. 完善CollectionView数据源方法
确保CollectionView能从twoDimensionalArrayList中拿到对应Section的数据,这里通过CollectionView的tag来关联TableView的Section:
// 返回CollectionView的item数量 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { let tableViewSection = collectionView.tag return twoDimensionalArrayList[tableViewSection].names.count } // 配置CollectionView的cell内容 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // 替换成你的CollectionViewCell标识符和类型 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DrinkTagCell", for: indexPath) as! DrinkTagCell let tableViewSection = collectionView.tag let drinkBrand = twoDimensionalArrayList[tableViewSection].names[indexPath.item] cell.tagLabel.text = drinkBrand // 替换成你cell中展示文本的控件 return cell }
3. 在TableViewCell中绑定CollectionView的Section标识
在TableView的cellForRowAt方法中,给内嵌的CollectionView设置tag为当前Section,确保数据对应:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // 替换成你的TableViewCell标识符和类型 let cell = tableView.dequeueReusableCell(withIdentifier: "DrinkSectionCell", for: indexPath) as! DrinkSectionCell // 绑定CollectionView的tag到当前Section cell.collectionView.tag = indexPath.section // 刷新CollectionView数据 cell.collectionView.reloadData() return cell }
4. 清理冗余代码
删除getDrinkDataFromJSON中所有处理subDrinkNameArray的代码,以及你不需要的subDrinkNameArray属性,避免干扰数据逻辑。
额外注意事项
- 确保
ExpandableDrinks模型的names属性是[String]类型,能正确接收预设的字符串数组。 - 检查TableView和CollectionView的复用标识符是否和代码中的一致,避免崩溃。
- 如果JSON返回的饮品类型数量超过预设数组的长度,代码中的容错逻辑会给对应Section设置空数组,你可以根据需求调整这个逻辑(比如显示提示文本)。
这样修改后,每个Section的CollectionView就能精准展示对应的预设子品牌数组啦!
内容的提问来源于stack exchange,提问作者Vinayak Bhor
相关产品推荐
相关产品推荐

