Sushil Dhital
Sushil Dhital

Reputation: 21

UITableView with Dynamic Sections, Custom Dropdown Views, and Repeated Data Issue on Scrolling

I'm working on a UITableView where each section has a single row containing custom dropdown views, text fields, etc. The issue I'm facing is that when I select multiple items from a dropdown view in one section and then scroll the table view, the selected data gets repeated in other sections. I need the data in my view controller file to make an api call with those data.

I've provided my custom dropdown view and the implementation of cellForRowAt for the table view below. I suspect it's a cell reuse issue, but I'm having trouble resolving it.

I have a custom dropdown view, separate file for UITableViewCell and a View Controller. I have passed the selected Items from my customDropdown View to Cell file using a delegate as follow:

chapterDropdownDelegate?.passChapterData(chapData: selectedItems)

Then i have again used the delegate to pass the data form cell file to view controller as follow:

func passChapterData(chapData: [DropDownModel]) {
                
        delegateChapter?.chapterDropdownView(chapData: chapData)
                
}

The cellForRowAt function is as follow:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellGenerateTestpaper", for: indexPath) as? CellGenerateTestpaper
                   
        cell?.questionTypeModel = questionTypeModel
        
        cell?.delegate = self
        
        cell?.delegateChapter = self
        
        cell?.delegateDifficulty = self
        
        cell?.section = indexPath.section

        cell?.isSelf = isSelfGrading
        
        cell?.isNormal = isNormal
        
        DispatchQueue.main.async {
            
            cell?.txtChapters.selectedDropDownItems = self.chapData
                        
        }
        
        cell?.updateDropdown(difficultyData: difficultyArray, chaptersData: chaptersArray)
        
        return cell ?? UITableViewCell()
        
    }

where updateDropdown function is to pass the api response dropdown list only,

self.chapData is the data selected from dropdown.

Any guidance on how to properly handle cell reuse and reset the state of custom dropdown views for each section would be greatly appreciated. Thanks!

I tried using prepareForResuse functions but didnt worked. When Scrolling slowly, the data does not repeat. If I remove DispatchQueue.main.async { cell?.txtChapters.selectedDropDownItems = self.chapData } this code, the selected data is lost whens scrolling table view.

Upvotes: 0

Views: 47

Answers (0)

Related Questions