Home > Software engineering >  Bug when reloading a certain row of a UTableView
Bug when reloading a certain row of a UTableView

Time:11-17

I have a strange bug when I reload a certain row of UITableView via the .reloadRows() method. Snippets of code:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return news_section.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell  =   tableView.dequeueReusableCell(withIdentifier: "cell_section", for: indexPath) as! NewsSectionCell
        cell.delegate_press_btn = self
        var news_index = self.news_section[indexPath.row]
        cell.number =  news_index
        cell.indexPath = indexPath
        return cell
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var highCell = self.needs_high_cell(news_sect: self.news_section[indexPath.row])
        return   highCell ? 150 : 90.0
    }

The cell has a button when pressed via delegate does following:

            self.news_section[index] = 0
            self.news_table_view.reloadRows(at: [indexPath], with: .none)

Sometimes it works sometimes it produces this: NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds

I tried to change the cell number by changing the array from where the cell gets his number. And then reload only that row.

CodePudding user response:

Use this code may be helpfull for you:---

let indexPath = IndexPath(item: rowNumber, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)

CodePudding user response:

For anybody looking for a similar problem :

// Reconfigures any existing cells for the rows. Reconfiguring is more efficient than reloading a row, as it does not replace the // existing cell with a new cell. Prefer reconfiguring over reloading unless you actually need an entirely new cell for the row. @available(iOS 15.0, *) open func reconfigureRows(at indexPaths: [IndexPath])

  • Related