title: Tableviewcontroller "cellForRowAtIndexPath" - index starts from 4, not 0
body: <p>I put three table view controllers on pageviewcontroller. </p>
  
  <p>The pageviewcontroller loads middle vc. </p>
  
  <p>When I go to the left vc, and pull table in order to reload - I have problem that on function cellForRowAtIndexPath the indexPath.row starts from 4, not 0. </p>
  
  <p>Why does the indexPath.row start from 4 and not 0? </p>
  
  <p>I think, it is not about code issue! I have error:</p>
  
  <blockquote>
    <p>Cannot index empty buffer</p>
  </blockquote>
  
  <p>I ve found the error. I dont know why does this happens but when I reload table (pulling it), for some reason, it takes the last row (in my case I have 4 rows on screen counting from 0) and uses it in cellAtIndex array. Every time before pulling data from internet I remove all elements from array. I changed that code, i did remove all elements before reloading and the error didnt appear. What's interesting I am using the same function on other vc and everything works.  </p>
  
  <p>I am making two request to download data:</p>
  
  <pre><code>class func JSONRequest2(urlInput: String, tableName: UITableView, action: (NSArray)-&gt;Void, refresh:UIRefreshControl, viewContr: UIViewController, hideLoadingViewAndStopAnimating: ()-&gt;Void) {
  
  
  
      let urlPath = urlInput
  
      let url = NSURL(string: urlPath)
  
      let session = NSURLSession.sharedSession()
  
      println("started first json request")
  
      let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -&gt; Void in
  
          if error != nil {
              println(error)
              let stringError = error.localizedDescription
              CommonFunctions.showAlert("Ошибка", alertText: stringError, alertButtonText: "Закрыть", viewController: viewContr)
          }
          else{
  
              var err: NSError?
              if data != nil {
  
                  if let jsonResult =  NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &amp;err) as? NSArray {
                      println("the number of news in json :\(jsonResult.count)")
  
                      if (err == nil) {
  
                          dispatch_async(dispatch_get_main_queue(), {
                              action(jsonResult)
                      //tableName.reloadData()
                          })
                      }else{
                          let stringError = err?.localizedDescription
                          CommonFunctions.showAlert("Ошибка", alertText: stringError!, alertButtonText: "Закрыть", viewController: viewContr)
                      }
                  }else {
                      hideLoadingViewAndStopAnimating()
                      println("json is not valid")
                      var dic = dictForErrors()
                      CommonFunctions.showAlert("Ошибка", alertText: dic.alertText, alertButtonText: "Закрыть", viewController: viewContr)
                  }
              }else {
  
                  CommonFunctions.showAlert("Ошибка", alertText: "data is nil",alertButtonText: "Закрыть", viewController: viewContr)
                  println("json data is nil")
              }
          }
      })
  
      task.resume()
  }
  
  class func JSONRequest(urlInput: String, tableName: UITableView, action: (NSArray)-&gt;Void, refresh:UIRefreshControl, category: Int, viewContr: UIViewController, hideLoadingViewAndStopAnimating: ()-&gt;Void) {
  
      //refresh.beginRefreshing()
  
      let urlPath = urlInput
  
      let url = NSURL(string: urlPath)
  
      let session = NSURLSession.sharedSession()
  
      println("started second request")
  
      let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -&gt; Void in
  
          if error != nil {
              println(error)
              let stringError = error.localizedDescription
              CommonFunctions.showAlert("Ошибка", alertText: stringError, alertButtonText: "Закрыть", viewController: viewContr)
          }
          else{
  
              var err: NSError?
              if data != nil{
                  if let jsonResult =  NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &amp;err) as? NSArray {
              println("the number of news in json :\(jsonResult.count)")
  
                      if (err == nil) {
  
  
  
                          dispatch_async(dispatch_get_main_queue(), {
                              action(jsonResult)
                              hideLoadingViewAndStopAnimating()
                              tableName.reloadData()
                              refresh.endRefreshing()
                          })
  
                      }else{
                          let stringError = err?.localizedDescription
                          hideLoadingViewAndStopAnimating()
                          CommonFunctions.showAlert("Ошибка", alertText: stringError!, alertButtonText: "Закрыть", viewController: viewContr)
                      }
                  }else {
                      hideLoadingViewAndStopAnimating()
                      println("json is not valid")
                      var dic = dictForErrors()
                      CommonFunctions.showAlert("Ошибка", alertText: dic.alertText, alertButtonText: "Закрыть", viewController: viewContr)
                  }
              }else{
  
                  CommonFunctions.showAlert("Ошибка", alertText: "data is nil",alertButtonText: "Закрыть", viewController: viewContr)
                  println("json data is nil")
              }
          }
      })
  
      task.resume()
  }
  </code></pre>
  
  <p>This is how I am making request in order to download two request</p>
  
  <pre><code>self.refreshControl = self.refreshController
      self.refreshControl?.addTarget(self, action: "loadDataNewsLenta", forControlEvents: .ValueChanged)
  
  
      if arrayMainPage.count &gt; 0 {
          activityView.alpha = 0.0
          arrayNewSLenta = arrayMainPage
          self.tableView.reloadData()
      }else{
          // loading first time news
          activityView.alpha = 0.5
          activityIndicator.startAnimating()
          isFirstReq = true
          arrayNewSLenta.removeAll(keepCapacity: false)
          CommonFunctions.JSONRequest2(urlString, tableName: tableView, action: desirializeJSONToArray, refresh: self.refreshController, viewContr: self, hidLoadingViewAndStopAnimating)
      }
  </code></pre>
  
  <p>I put the code above on viewdidload. This code is where I am reloading :</p>
  
  <pre><code>func hidLoadingViewAndStopAnimating() {
      activityView.alpha = 0
      activityIndicator.stopAnimating()
  }
  
  func configureTableView() {
      tableView.rowHeight = UITableViewAutomaticDimension
      tableView.estimatedRowHeight = 110.0
  }
  
  func loadDataNewsLenta() {
      arrayNewSLenta.removeAll(keepCapacity: false)
      isFirstReq = true
      refreshController.beginRefreshing()
      CommonFunctions.JSONRequest2(urlString, tableName: tableView, action: desirializeJSONToArray, refresh: self.refreshController, viewContr: self, hidLoadingViewAndStopAnimating)
  }
  </code></pre>
  
  <p>This is all about tableview code and desirialization of json to array:</p>
  
  <pre><code> // MARK: - Table view data source
  
  override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
      // #warning Potentially incomplete method implementation.
      // Return the number of sections.
      return 1
  }
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      println("it is news lenta count \(arrayNewSLenta.count)")
      return arrayNewSLenta.count
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell = self.tableView.dequeueReusableCellWithIdentifier("lentaCell", forIndexPath: indexPath) as LentaTableViewCell
      let inex = indexPath.row
      cell.labelDateOfPublication.text = arrayNewSLenta[indexPath.row].pubDate
      cell.labeltitle.text = arrayNewSLenta[indexPath.row].pageTitle
  
      return cell
  }
  
  func desirializeJSONToArray(jsonArray: NSArray){
  
      for singleJSON in jsonArray{
          var singleArticle = ArticleInfo()
  
          if let arrayText = singleJSON as? NSDictionary{
  
              if let articleID = arrayText["id"] as? NSString{
                  singleArticle.articleID = articleID
              }
  
              if let pageTitle = arrayText["pagetitle"] as? NSString {
                  singleArticle.pageTitle = pageTitle
              }
  
              if let longTitle = arrayText["longtitle"] as? String{
                  singleArticle.longTitle = longTitle
              }
              if let introText = arrayText["introtext"] as? String{
  
                  singleArticle.introText = introText
              }
              if let contentText = arrayText["content_text"] as? String{
                  singleArticle.contentText = contentText
  
              }
              if let category = arrayText["category"] as? String{
  
                  singleArticle.category = category
              }
              if let imageLink = arrayText["thumbnail"] as? String{
                  singleArticle.linkToImage = imageLink
  
              }
              if let videoLink = arrayText["video"] as? String{
                  singleArticle.videoLink = videoLink
                  println("hre is video link")
                  println(videoLink)
              }
              if let sity = arrayText["sity"] as? String{
  
                  singleArticle.sity = sity
              }
              if let visible = arrayText["visible"] as? String{
                  singleArticle.visible = visible
  
              }
              if let visits = arrayText["visits"] as? String{
  
                  singleArticle.visits = visits
              }
              if let pubDate = arrayText["pubdate"] as? String{
  
                  singleArticle.pubDate = pubDate
              }
  
              // insert result into array
              arrayNewSLenta.append(singleArticle)
          }
      }
      //cycle ended
      if isFirstReq == true {
  
          let urlStringSecondRequest = "http://www.kfdz/artifdcles/JsonMainList"
          CommonFunctions.JSONRequest(urlStringSecondRequest, tableName: tableView, action: desirializeJSONToArray, refresh: self.refreshController, category: 0, viewContr: self, hidLoadingViewAndStopAnimating)
      }
      isFirstReq = false
  }
  </code></pre>
  
  <p>The interesting fact I noticed , when I go to first vc and pull table immediately, for some reason it load cellAtIndex function using only the last row on screen(in my case index 4). on the other hand, when I go to first vc and choose some item (to see detailed view) and go back and pull table everything works. 
  On more issue to say, when I change transition style of uipageviewcontroller to PageCurl I dont have this problem!  </p>