开发者

numberOfSectionsInTable isn't called when [arr count] is returned

When I try returning the count of my newsItems array it breaks my application. I used NSLog to find out what the value of the newsItems count was when I returned 1.

Here is from NSLog 2010-04-13 07:48:40.656 RSS Feed[453:207] Values of num items 31 2010-04-13 07:48:40.656 RSS Feed[453:207] Number o开发者_StackOverflow社区f items 31

Does anyone know why returning [newsItems count] would cause my application to break?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"Values of num items %d",[newsItems count]);
NSInteger *length = (NSInteger *)[newsItems count];
NSLog(@"Number of items %d",length);

return 1;
//return [newsItems count]; //This keeps breaking it for some reason

}


Very silly problem I found in your code.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"Values of num items %d",[newsItems count]);
NSInteger *length = (NSInteger *)[newsItems count];
NSLog(@"Number of items %d",length);

return 1;
//return [newsItems count]; //This keeps breaking it for some reason

See, NSInteger *length = (NSInteger *)[newsItems count]; is having a problem here.

You need to write this

NSInteger length = [newsItems count];

return length;

NSInteger is a typedef ( other name ) of integer variable it's not a class - ( that's the reason that you don't require '*' before it's variable )


  1. Why is your NSInteger a pointer type? The line declaring and initializing length should be

    NSInteger length = [newsItems count];

  2. Did you check whether the issue is in another method, maybe numberOfRows or cellForRow?


Are you certain that you need numberOfSectionsInTableView and not numberOfRowsInSection? numberOfSectionsInTableView, if not implemented, returns 1.

If so, remember that if you implement numberOfSectionsInTableView, you also need to implement numberOfRowsInSection. Remember, after getting the section count from you, next thing tableview will do is ask you for the number of rows in each section, and then for item in each row in each section.

Also, what lostInTransit mentioned: don't try to return a pointer to NSInteger. In fact, I thing returning an int will be valid as well, and that int will get implicitly cast. Otherwise return 1; would not work, right?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜