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

1.5k Views Asked by At

Am developing a app using swift in my in one controller am using both collection view and table view.collection view which kept at top of the controller which scrolls horizontally and contains near 10 cells and table view is just kept below to the collection view.i have passed the json(api) data to both collection and table view now i have to call another json data(another api) which is subcategory of before first json data and then if i tapp the cell of collection view it should call the second api and reload the table view again to display second api data...how to do this task??

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

my datasource method of table and 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

It helps to call first api and passing data.now am having another api for that I have written different function:

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

}

both the json(api) has same key value pair now by tapping cell of collection view should call the second api function and reload my table view according to the second api json data.

mr.bista answer should be accepted....

Just we need to remove the array i.e.,myarray.removeallobjects()

1

There are 1 best solutions below

4
On BEST ANSWER

Basic skeleton:

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
     }

   }

}