1

I'm trying to implement infinite scrolling in UITableView by checking the indexpath at "willDisplay cell", however once the table is loaded with data, the "willDisplay cell" func seems to be drawing all cells before I scroll down although the mobile screen can only fit one or two cells at a time.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print(indexPath.row)
}

Output without scrolling down:

0
1
2
3
4
5

When I scroll down the "willDisplay cell" func works properly and it prints the indexpath.row one at a time.

What could be the problem here?

Here's a look at the full code:

override func viewDidLoad() {
        super.viewDidLoad()
        postsTableview.delegate = self
        postsTableview.dataSource = self
        let nibName = UINib(nibName: "postTableViewCell", bundle:nil)
        postsTableview.register(nibName, forCellReuseIdentifier: "postTableViewCell")

        loadPosts()           
    }


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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "postTableViewCell", for: indexPath) as! postTableViewCell
        return cell
    }

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print(indexPath.row)
}

I have tried setting the tableview cell height fixed with large value that exceeds the screen height but I still have the same problem

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 1000.0;
    }
Ahmed Elmasry
  • 359
  • 2
  • 13
  • 1
    what do you mean by _"drawing all cells at the same time"_? every cell is draw individually. – holex Nov 28 '17 at 14:34
  • What's the behavior you expect? It sounds to me like, when the screen loads, your table view has room to display 5 cells, so you get the console output you mention. – Connor Neville Nov 28 '17 at 14:34
  • No the screen can only fit one cell – Ahmed Elmasry Nov 28 '17 at 14:35
  • Here's a look at how the screen can fit only one cell at a time https://imgur.com/a/o1VJ9 – Ahmed Elmasry Nov 28 '17 at 14:38
  • @holex if every cell is drawn individually when it's about to appear on the screen, why the console output shows all the indexpath numbers at the same time without scrolling down while the screen size can fit only cell – Ahmed Elmasry Nov 28 '17 at 15:06
  • if your table-view fit one cell only, then your method _would_ be invoked once. your table-view apparently hosts 6 cells and your method is invoked 6 times, once for each cell. – holex Nov 28 '17 at 15:09
  • Where are you setting the row height? Are these auto-sizing cells? From the image you link, it looks like *at least* two cells are visible (I'm assuming the *aljezeera.net* label and round-image are at the top of the next row?). – DonMag Nov 28 '17 at 15:24
  • Yes these are auto sizing cells and yes two cells are visible – Ahmed Elmasry Nov 28 '17 at 16:54
  • @AhmedElmasry - ok, then show your actual code... what you posted in your question is not setting up the table for auto-sizing cells. – DonMag Nov 28 '17 at 19:49
  • @DonMag I have tried fixed height with large value and I still get the same problem func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 1000.0; } – Ahmed Elmasry Nov 29 '17 at 07:57
  • What tableview's willDisplay method say?: It say wiil display cell at indexPath. So how you can scroll before it display????? – SPatel Nov 29 '17 at 09:31
  • For infinite scroll please first search question. https://stackoverflow.com/questions/39215081/swift-ios-add-infinite-scroll-pagination-to-uitableview – SPatel Nov 29 '17 at 09:38
  • @AhmedElmasry - You said *"screen can only fit one cell"* but it can fit parts of 2 (or maybe 3). You said *"these are auto sizing cells"* but you don't show code for auto-sizing cells. **Obviously** you are doing something in your code that is causing the problem - but if you don't show what you're doing, how can anybody help? Refer to this page: https://stackoverflow.com/help/mcve – DonMag Nov 29 '17 at 13:20

2 Answers2

2

It turned out that I needed to set an estimated row height. I'm coming from Android background and this sounds silly to me but it worked out.

postsTableview.estimatedRowHeight = 400
postsTableview.rowHeight = UITableViewAutomaticDimension
Ahmed Elmasry
  • 359
  • 2
  • 13
0

On your edited question:

There's nothing wrong with iOS, all table/collection view cells are drawn before they are shown; imagine a table view that shows nothing in the beginning when it's shown, would that be weird?

I don't know what you are trying to achieve after the cells are shown, but I believe that's suitable for another question, good luck!

Bright Future
  • 4,961
  • 2
  • 47
  • 67
  • I didn't mean literally at once, I just mean they are drawn before I scroll down even though the screen can fit only one or two cells at a time – Ahmed Elmasry Nov 29 '17 at 09:08
  • @AhmedElmasry Ah I get it, change your question man, you have to be exact on SO to get accurate answers. – Bright Future Nov 29 '17 at 14:28
  • On your edited answer: Why people use willdisplay cell method to achieve infinite scrolling in tableview though? https://stackoverflow.com/questions/34588837/uitableview-load-more-when-scrolling-to-bottom – Ahmed Elmasry Dec 02 '17 at 07:14