Link to home
Start Free TrialLog in
Avatar of Mahan Tabatabaie
Mahan Tabatabaie

asked on

Store Heavy UITableViewCell in Array instead of Dequeue

I had a problem with UITableView Lagging on Scroll when there were long non-English (Persian) text in the cell (list with more than 40000 items and text with more than 1500 chars wont lag if they are English ) so I came up with this only solution that worked for me to keep the cells in an Array (just the non-English Ones) and reuse them myself instead of dequeuing again (will use normal pattern with other cell types) it works just perfect when the data is already added for the first time , adding a new Data no matter of what type messes up the table. here's the code :

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


            if(dataIndex[indexPath.row].cell == nil)
            {
                let cell = tableView.dequeueReusableCell(withIdentifier: "ChatRightTextCell", for: indexPath) as! ChatRightTextCell


                cell.configureCell(indexPath: indexPath)
                dataIndex[indexPath.row].cell = cell
                return cell
            }
            else
            {
                let cell = dataIndex[indexPath.row].cell
                cell.configureCell(indexPath: indexPath)
                return cell
            }

        }

note that the Table and the cells are transformed by 180 degree so the cells are stacked from bottom , so adding a new Item to the table be like :

dataIndex.insert(newData,at:0)
tableview.reloadData()


at ViewController start there's a 'for' adding many items like above it works fine without any lag for those specific cells , adding a new Data now will mess up the table

any solution ?

NOTE : please don't suggest other UItableView Patterns I know this might not be the best solution but its working perfect in my case and as I mentioned above the list with more than 40000 items with English text is working fine so I know how to create a TableView .
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.