gpt4 book ai didi

ios - 如何通过快速点击 Collection View 的单元格来重新加载 TableView 中的数据

转载 作者:行者123 更新时间:2023-11-30 12:48:44 26 4
gpt4 key购买 nike

我正在一个 Controller 中使用 swift 开发一个应用程序,同时使用 Collection View 和 TableView 。 Collection View 保持在 Controller 顶部,水平滚动并包含近 10 个单元格,而 TableView 则保持在集合下方view.i 已将 json(api) 数据传递给集合和 TableView ,现在我必须调用另一个 json 数据(另一个 api),它是第一个 json 数据之前的子类别,然后如果我点击 Collection View 的单元格,它应该调用第二个 api 并再次重新加载 TableView 以显示第二个 api 数据...如何执行此任务?

func jsonParsingFromURL () {
Alamofire.request(.POST, "http://something.com", parameters: nil, encoding: .URL, headers: nil).response { (req, res, data, error) -> Void in
// print(res)
// print(data)

let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
print(dataString)
self.startParsing(data!)
self.collecStartParsing(data!)
}
}
func startParsing(data :NSData)
{
let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
for i in 0 ..< (dict.valueForKey("subcategory") as! NSArray).count
{
arrDict.addObject((dict.valueForKey("subcategory") as! NSArray) .objectAtIndex(i))
}
SecondTableView.reloadData()
}

func collecStartParsing(data :NSData){
let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
for i in 0 ..< (dict.valueForKey("subcategory_icons") as! NSArray).count
{
collectionitems.addObject((dict.valueForKey("subcategory_icons") as! NSArray) .objectAtIndex(i))
}
FirstCollectionView.reloadData()
}

我的表和 Collection View 的数据源方法:

func tableview cell for row at indexpath {

let cell = tableView.dequeueReusableCellWithIdentifier("SegueCell") as! SecondTableViewCell

let strTitle:NSString = arrDict[indexPath.row] .valueForKey("adtitle") as! NSString

cell.TitleLabel.text = strTitle as String

return cell

Collection View :

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FirstCollectionView", forIndexPath: indexPath) as! FirstCollectionViewCell


let strTitle:NSString = collectionitems[indexPath.item] .valueForKey("subcategoryname") as! NSString


cell.TitleLabel.text = strTitle as String

return cell

它有助于调用第一个 api 并传递数据。现在我有另一个 api,我为此编写了不同的函数:

func jsonParsingForIcon () {
Alamofire.request(.POST, "http://www.mysecondapi.com", nil, encoding: .URL, headers: nil).response { (req, res, data, error) -> Void in
// print(res)
// print(data)

let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
print(dataString)
}

}

现在,通过点击 Collection View 的单元格,两个 json(api) 都具有相同的键值对,应该调用第二个 api 函数并根据第二个 api json 数据重新加载我的 TableView 。

bista先生的回答应该被接受......

只是我们需要删除数组,即 myarray.removeallobjects()

最佳答案

基本骨架:

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let category = categoryArray[indexPath.row]
myAPI(category.id)
}

func myAPI(categoryID:String) {
//pass the categoryID into the parameter to get corresponding data.
callAPI {
if success {
tableArray = data from server
tableView.reloadData()
} else {
//some error occurred
}

}

}

关于ios - 如何通过快速点击 Collection View 的单元格来重新加载 TableView 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41295755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com