簡體   English   中英

如何通過快速點擊集合視圖的單元格在表視圖中重新加載數據

[英]How to reload data in table view by tapping the cell of collection view in swift

我正在一個控制器中使用swift開發一個應用程序,同時使用了集合視圖和表視圖。集合視圖位於控制器的頂部,該控制器水平滾動並包含近10個單元格,而表視圖則保持在集合視圖的下方。已將json(api)數據傳遞到集合和表視圖中,現在我必須調用另一個json數據(另一個api),該子類是第一個json數據之前的子類別,然后如果我輕按集合視圖的單元格,則應調用第二個api並再次重新加載表格視圖以顯示第二個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()
}    

我的表和集合視圖的數據源方法:

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

集合視圖:

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)
    }

}

現在,兩個json(api)都具有相同的鍵值對,通過點擊collection view的單元格,應調用第二個api函數並根據第二個api json數據重新加載我的表視圖。

比斯塔先生的答案應該被接受。

只是我們需要刪除數組,即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
     }

   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM