0

I have seen a lot of answers with my query, but nothing solved mine. I am opening a new question here.

I have an API, which returns data. I am showing it in the header and the cell. And, there is a load more button too, if the API returns 1 for has_more key. So, when I tap on the load more button I call that API again so that I can get the leftover data, but it refreshes the entire table view as well because I reload the table view.

So, now, what I want to do is I want to append the new cells and headers without reloading the existing table view so that the previous headers and cell in the tableView are not affected.

This is the API function:

func getOverallWinners(has: Int)
{
    let params = ["api_token": Constants.USER_INFO["api_token"].rawValue,"has_more": has]

    ServiceHelper.sharedInstance.sendRequest(path: "contest-overall-winning", params: params, showSpinner: true)
    { (response, error) in

        if error != nil
        {
            print("Error: \(error.debugDescription)")
        }
        else
        {
            self.winnerArr     = response["result"].arrayObject as? Array<Dictionary<String,Any>>

            self.overallL.text = "$\(String(describing: response["overall"].rawString()!))"

            self.winningTbl.beginUpdates()

            let indexPath:IndexPath = IndexPath(row:((self.winnerArr?.count)! - 1), section:0)

            self.winningTbl.insertRows(at: [indexPath], with: .left)

            self.winningTbl.endUpdates()

            self.winningTbl.scrollToRow(at: indexPath, at: .bottom, animated: true)

            let loadMore = response["has_more"].rawValue as? Int

            if  loadMore == 0
            {
                self.constantHeight4LoadMore.constant = 0

            }
            else
            {
                self.constantHeight4LoadMore.constant = 40
            }

        }
    }
}

Here are te tableView Delegate:

    func numberOfSections(in tableView: UITableView) -> Int {

    return (winnerArr?.count)!
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let rowArray =  winnerArr![section]["response"] as? Array<Dictionary<String,Any>>

    return rowArray!.count

}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 60
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    return 60
}


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    self.prizeArr.removeAll()

    let header =  tableView.dequeueReusableHeaderFooterView(withIdentifier: cellReuseIdentifier) as! OverallHeaderCell

    let resArr   = self.winnerArr![section]["response"]! as? Array<Dictionary<String,Any>>

    for prize in resArr!
    {
        let doubleStr = prize["prize"] as? NSString

        self.prizeArr.append((doubleStr?.doubleValue)!)
    }

    let sumedStr = prizeArr.reduce(0, +)

    header.textL[0].text = winnerArr![section]["name"] as? String

    header.textL[1].text = "$\(sumedStr)"

    header.textL[3].text = "\(String(describing: resArr!.count))"

    return header
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "owCell", for: indexPath) as! OWCell

    let response = winnerArr![indexPath.section]["response"] as? Array<Dictionary<String,Any>>

    cell.collectionL[0].text = (response![indexPath.row]["name"] as! String)

    cell.collectionL[1].text = "$\(String(describing:response![indexPath.row]["prize"]!))"

    cell.collectionL[2].text = "WIN"

    cell.selectionStyle        = .none

    return cell
}

I tried the beginUpdates() above but nothing worked as every time I will click the load more button I will receive new headers. In those headers there will be the new cells. Can anyone help?

This is not a duplicate beacause that solution is for inserting new rows in a section, but here I have to first create new sections and then add new rows in them. I do not watnt to touch the old sections.

Rob13
  • 381
  • 8
  • 20
  • `insertRows` and `insertSections` are the functions to achieve what you want. What exactly doesn't work? – mag_zbc Aug 31 '18 at 06:45
  • Possible duplicate of [How to insert new cell into UITableView in Swift](https://stackoverflow.com/questions/31870206/how-to-insert-new-cell-into-uitableview-in-swift) – mag_zbc Aug 31 '18 at 06:47
  • @mag_zbc It throws this error `Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 0 beyond bounds for empty array'` – Rob13 Aug 31 '18 at 06:50
  • Where exactly does this error occur? – mag_zbc Aug 31 '18 at 06:53
  • It throws me to App Delegate every time and in the terminal, I get this error. – Rob13 Aug 31 '18 at 06:55

0 Answers0