aawige
aawige

Reputation: 1

UITableview takes multiple scrolling attempts before scrolling

I have a UITableView that uses a custom cell. My issue is that scrolling is unresponsive to my first and often second and third attempts. I almost always have to swipe up multiple times before it will actually scroll down. It scrolls smoothly once it gets moving, but it takes multiple scrolls to get it started again once it stops.

class TableCell: UITableViewCell {

    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var cardNameLabel: UILabel!

    func configure(withItem card: Networking.Card) {

    }
}
class collectionTableDataSource: NSObject, UITableViewDataSource {

    var cardData: [Networking.Card] = []

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cardData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
        cell.cardNameLabel.text = cardData[indexPath.row].cfDisplayName
        return cell
    }
}
class TableVC: UIViewController {

@IBOutlet weak var collectionTable: UITableView!
var dataSource: collectionTableDataSource!

    override func viewDidLoad() {
        super.viewDidLoad()

    dataSource = collectionTableDataSource()
    collectionTable.dataSource = dataSource

    Networking.shared.getCardsForUser(user: "1") { (response) in
            self.dataSource.cardData = response
            self.collectionTable.reloadData()
        }
}

Upvotes: 0

Views: 114

Answers (2)

aawige
aawige

Reputation: 1

I isolated the issue to a timer on the previous view controller. The timer runs every 5 seconds and makes a networking request. I fixed the problem by invalidating the timer before segue to the view controller with the UITableview. Now scrolling runs smoothly.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "firstSegue" {
            self.timer?.invalidate()
        }
    }

Upvotes: 0

Legonaftik
Legonaftik

Reputation: 1390

I guess the problem is at this part of your code:

Networking.shared.getCardsForUser(user: "1") { (response) in
    self.dataSource.cardData = response
    self.collectionTable.reloadData()
}

Please, make sure that the completion handler of your async request is performed on the main queue. It's required because you're trying to reload UITableView. Any UI-related work must be done at DispatchQueue.main

So you can try the following:

Networking.shared.getCardsForUser(user: "1") { response in
    DispatchQueue.main.async {
        self.dataSource.cardData = response
        self.collectionTable.reloadData()
    }
}

Upvotes: 0

Related Questions