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

标签 ios json swift tableview collectionview

我正在一个 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/

相关文章:

android - 如何在 Android 和 iOS 应用程序中使用 WhatsApp 登录?

ios - 指定 maven nar 插件编译器和链接器可执行路径

Java - 使用 Gson 库从 servlet 返回 JSON

android - Qt Android App 无法通过 XMLHttpRequest 连接到互联网

swift - (存档)Swift Range 支持 float 吗?

swift - 对象作为错误 - CoreData 关系

ios - Storyboard启动检查的最佳实践

ios - 调用 popToViewController 的正确方法 - iOS

java - Android 在将 json 发布到 web 服务时发送空参数

swift - 在 Swift 中将汉堡包按钮添加到 SWRevealViewController