开发者

UITableView numberOfRowsInSection section order problem

I am having a problem while inserting data in a table view. I have made this little example demonstrating my problem:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            NSLog(@"section: %d",section);
            if([listOfItems count]!=0){
                NSLog(@"size: %d",[listOfItems count] / 2);
                return [listOfItems count] / 2;
            }
            else{
                sectionCount++;
                NSLog(@"size: %d", [[listOfComboBoxes objectAtIndex:0] count] - 1);
                return [[listOfComboBoxes objectAtIndex:0] count] - 1;
            }
            break;
        default:
            NSLog(@"section: %d",section);
            NSLog(@"size: %d", [[listOfComboBoxes objectAtIndex:sectionCount++] count] - 1);
            sectionCount--;
            return [[listOfComboBoxes objectAtIndex:sectionCount++] coun开发者_C百科t] - 1;
            break;
    }
}

This is what i receive in the log:

2011-06-09 11:14:40.753 TemplatesTest[1133:207] section: 3
2011-06-09 11:14:40.754 TemplatesTest[1133:207] size: 2
2011-06-09 11:14:40.754 TemplatesTest[1133:207] section: 0
2011-06-09 11:14:40.755 TemplatesTest[1133:207] size: 6
2011-06-09 11:14:40.755 TemplatesTest[1133:207] section: 1
2011-06-09 11:14:40.756 TemplatesTest[1133:207] size: 11
2011-06-09 11:14:40.756 TemplatesTest[1133:207] section: 2
2011-06-09 11:14:40.757 TemplatesTest[1133:207] size: 20

I am wondering why is it starting with section 3? The correct output should be (if it started with section 0):

section: 0
size: 6
section: 1
size: 2
section: 2
size: 11
section: 3
size: 20

Does anyone knows a fix?


Why is it starting with section 3? The real question is: why are you assuming that it should start with section zero? There is nothing in the documentation that defines the order that the method is called and so you should not make any assumptions. In current implementations it starts at the bottom of the table and works up, but, again, that's an assumption and making it could get you into trouble in the next version of iOS.

The solution is to write the method so it can return the correct result whatever the order of the requests are.


There is no fix as this isn't a problem. The order in which the tableview requests data and draws it makes no difference to your code.

You should be using the indexPath.section and indexPath.row properties to determine what information the tableview wants.


Why are you caring about the order in which the data source methods are called? The order in which the section details are navigated by the SDK will not affect the behavior or the interface of your app.

The non-ordered calls could probably depend on the fact that the arrays that the SDK internally uses are not ordered. But, again, you should never worry about this "issue".

You state that the correct output should be the ordered one, but why?

Edit

What you are doing wrong here is supposing an order to be where no-one guarantees you that. You should really implement a switch construct with a different case for each section you want to control. The data source contents do have to be deterministic, and should not change dynamically without control.

Delegating all the appearance to an autoincrementing value that updates depending on the order in which that function is called is not the way you should handle things.


You will find the same question with extra information here

http://www.iphonedevsdk.com/forum/iphone-sdk-development/70913-numberofsectionsintableview-called-twice-uitableview.html

But to my assumption, UITableView creates the cell from bottom to top so that is the reason or not the delegate and datasource methods are called for rows and sections that are at the bottom of the screen and not at the end of uitableview

Edit:

May be you can calculate the number of rows before hand for each section in view did load and then just pass the value in this method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜