4

I have a UITableView and I have one of the required methods for its execution:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    println("section is \(section)")
    println(self.items.count) // self.items.count = 3
    return self.items.count
}

In my log I see the method being run 3 times:

section is 0
3
section is 0
3
section is 0
3

EDIT:

This is the snippet of my section:

func numberOfSectionsInTableView(tableView:UITableView!)->Int
{
    return 1
}

Picture of my thread stack: https://i.stack.imgur.com/5USNn.png

jshah
  • 1,599
  • 2
  • 17
  • 38
  • 1
    Set a breakpoint and see why/when it is being called each time. – rmaddy Jan 26 '15 at 04:36
  • 1
    Do you have multiple sections? – Doug Watkins Jan 26 '15 at 04:41
  • I only have 1. I added the snippet for that code in the edit section of the original post. When I put a breakpoint on both of these guys, it runs numberOfSectionsInTableView() first then tableView(), it does that process 3 times. – jshah Jan 26 '15 at 04:52
  • @DougWatkins He better not since the result is based off of a single array. – rmaddy Jan 26 '15 at 04:52
  • In my experience I always see 2 prints to the console. I always chucked it on the method checking for section 0 and then checking if there is another section. – Tokuriku Jan 26 '15 at 04:59
  • Per @rmaddy's comment, what is calling these methods? Is it different each time they are called? – Doug Watkins Jan 26 '15 at 05:03
  • I added println("section is \(section)") and I get section is 0 all 3 times – jshah Jan 26 '15 at 05:14
  • @DougWatkins I am setting a breakpoint in the method, how can I tell what is calling them? Aren't these the default methods that have to be set when using a UITableViewDelegate/UITableViewSource protocol – jshah Jan 26 '15 at 05:15
  • They are, but there are things you can do that will cause them to be run. When the breakpoint is reached, the stack, on the left side of Xcode, should show what methods called what, all the way to the main(). – Doug Watkins Jan 26 '15 at 05:27
  • 1
    Where are you calling reloadData on the tableView? Each time you call it, the method will be called for each section. – Josh Gafni Jan 26 '15 at 05:55
  • @JoshGafni not calling reloadData() anywhere – jshah Jan 26 '15 at 07:46
  • @DougWatkins I've attached a image to the original post of my thread stack – jshah Jan 26 '15 at 07:47

1 Answers1

2

In the UITableView there are many situations, where different delegate-methods will get called multiple times due to something like auto-refreshing of the tableview.

Check this answer for more informations.

Community
  • 1
  • 1
Christian
  • 22,585
  • 9
  • 80
  • 106