1

I have an array of strings in which I store strings, that later I add to the URL for the .GET request on Alamofire. I use a for loop to iterate through the array so for each element Alamofire would make a .GET request and receive a JSON that after being parse I will append to another string.

The fact is that I have a CollectionView inside a TableViewCell, what I do is create a cell for each of the elements in the forenamed array, and a row of CollectionView with the content that I get from the .GET request the problem is that the name of the TableViewCell DOES NOT match the content appearing on the CollectionView.

This is the code that I am using at the moment:

func downloadRadios(){
        for names in browse{
            index.append(names)
            let url = "http://api.dirble.com/v2/search/\(names)?token="

            Alamofire.request(.GET, url).validate().responseJSON { response in
                switch response.result {
                case .Success(let data):
                    let json = JSON(data)

                    if let arr = json.arrayObject as? [[String:AnyObject]] {
                        var myRadio = [String]()

                        for items in arr{
                            let name = items["name"] as? String
                            myRadio.append(name!)

                        }
                        print(myRadio)
                        self.radio.append(myRadio)
                        self.tableView.reloadData()

                    }
                case .Failure(let error):
                    print("Request failed with error: \(error)")
                }
            }
        }
        print("radio vale\(radio.count)")

    }

Code to populate the TableView:

func tableView(tableView: UITableView,
                   cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("PopularRadioTableViewCell", forIndexPath: indexPath) as! PopularRadioTableViewCell
        cell.configureCell(index[indexPath.row])
        return cell
    }

Code to populate the CollectionView:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PopularRadioCell", forIndexPath: indexPath) as! PopularRadioCell
        cell.configureCell(radio[collectionView.tag][indexPath.item], image:"hey")
        return cell
    }
CTABUYO
  • 662
  • 2
  • 7
  • 27
  • Check this: http://stackoverflow.com/questions/26277371/swift-uitableview-reloaddata-in-a-closure – Dharmesh Kheni Aug 13 '16 at 17:35
  • @DharmeshKheni still same problem – CTABUYO Aug 13 '16 at 17:39
  • `the name of the TableViewCell DOES NOT match the content appearing on the CollectionView.` what this means? – Dharmesh Kheni Aug 13 '16 at 17:49
  • @DharmeshKheni That essentially means that the for loop goes faster than Alamofire. So the results are not appended in the array with the same order as the array I´m iterating. – CTABUYO Aug 13 '16 at 17:51
  • @DharmeshKheni I have provided enough code. – CTABUYO Aug 13 '16 at 17:54
  • If you want the same order, create a custom class as model and include the Alamofire request in the model. Then create instances of the class, populate the data source array in the order you want and perform the download by calling the method in the model respectively. – vadian Aug 13 '16 at 17:59

0 Answers0