user1960169
user1960169

Reputation: 3653

How to know UITableView has finished loading data in swift

I am loading my UITableView using an Arrayin swift. What I want to do is after table has loaded my array should be ampty (want to remove all object in the array then it loads another data set to load another table view)

What I want to do is adding several UItables dinamically to a UIScrollView and load all the data to every UITableView initially. Then user can scroll the scrollview horizontally and view other tables.So in my ViewDidLoadI am doing something like this.

 for i in 0..<dm.TableData.count {

        self.catID=self.dm.TableData[i]["term_id"] as? String
        self.jsonParser()
    }

then this is my jsonParser

func jsonParser() {



    let urlPath = "http://www.liveat8.lk/mobileapp/news.php?"
    let category_id=catID
    let catParam="category_id"
    let strCatID="\(catParam)=\(category_id)"

    let strStartRec:String=String(startRec)
    let startRecPAram="start_record_index"
    let strStartRecFull="\(startRecPAram)=\(strStartRec)"


    let strNumOfRecFull="no_of_records=10"

    let fullURL = "\(urlPath)\(strCatID)&\(strStartRecFull)&\(strNumOfRecFull)"
    print(fullURL)


    guard let endpoint = NSURL(string: fullURL) else {
        print("Error creating endpoint")
        return
    }

    let request = NSMutableURLRequest(URL:endpoint)
    NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
        do {
            guard let data = data else {
                throw JSONError.NoData
            }
            guard let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary else {
                throw JSONError.ConversionFailed
            }
           print(json)
            if let countries_list = json["data"] as? NSArray
            {

                for (var i = 0; i < countries_list.count ; i++ )
                {
                    if let country_obj = countries_list[i] as? NSDictionary
                    {
                        //self.TableData.append(country_obj)
                        self.commonData.append(country_obj)


                        }
                }

                //self.updateUI()
                if self.commonData.isEmpty
                {

                }
                else
                {
                    self.updateUI()
                }



            }

            } catch let error as JSONError {
            print(error.rawValue)
        } catch let error as NSError {
            print(error.debugDescription)
        }
        }.resume()


}

Then UpdateUI()

func updateUI()
{


  print("COMMON DATA ARRAY\(self.commonData)")
   // print("NEWS DATA ARRAY\(self.newsNews)")
    //print("SPORTS DATA ARRAY\(self.sportsNews)")
    let tblY:CGFloat=segmentedControl.frame.origin.y+segmentedControl.frame.size.height
    tblNews=UITableView.init(frame: CGRectMake(x,0 , self.screenWidth, self.screenHeight-tblY))
    tblNews.tag=index
    tblNews.delegate=self
    tblNews.dataSource=self
    tblNews.backgroundColor=UIColor.blueColor()

    self.mainScroll.addSubview(tblNews)
    x=x+self.screenWidth




    index=index+1

    tblNews.reloadData()


}

`UITableView` use this `commonData` array as the data source. Now when I scroll table view data load with previous data too.So what is the best way to do this? or else please tell me how can use `self.commonData.removeAll()` after 1 `UITableView` has loaded.Currently I did in `CellforrowAtIndex`

if indexPath.row == self.commonData.count-1
    {
        self.commonData.removeAll()
    }


    return cell

but this doesn't solve my problem

Upvotes: 0

Views: 1109

Answers (1)

user212514
user212514

Reputation: 3130

You should have separate sets of data, possibly arrays, for each UITableView. iOS will call back to your datasource delegate methods to request data.

It is important that you not delete data from the arrays because iOS is going to call your data source delegate methods expecting data. Even if you display the data in the table views initially, the user may scroll the scroll view causing one of the UITableView's to call your delegate methods to get the data again.

The data source delegate methods, such as func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell have a UITableView parameter that you can use to determine which data source is appropriate.

For example, you might have:

self.commonData1 = ["a", "b", "c"]
self.commonData2 = ["d", "e", "f"]

And you need to keep track of any tables you add to your scroll view:

self.tableView1 = ...the table view you create & add to scroll view
self.tableView2 = ...the table view you create & add to scroll view

And when you're responding to data source calls:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
   if tableView == self.tableView1 {
      return 1
   } else if tableView == self.tableView2 {
      return 1
   }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   if tableView == self.tableView1 {
      return self.commonData1.count
   } else if tableView == self.tableView2 {
      return self.commonData2.count
   }
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! IdeaTableViewCell
   if tableView == self.tableView1 {
      cell.textLabel?.text = self.commonData1[indexPath.row]
   } else if tableView == self.tableView2 {
      cell.textLabel?.text = self.commonData2[indexPath.row]
   }

   return cell
}

Upvotes: 1

Related Questions