aahrens
aahrens

Reputation: 5590

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 of 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

}

Upvotes: 1

Views: 755

Answers (3)

sagarkothari
sagarkothari

Reputation: 24810

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 )

Upvotes: 1

Ivan Vučica
Ivan Vučica

Reputation: 9669

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?

Upvotes: 0

lostInTransit
lostInTransit

Reputation: 70997

  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?

Upvotes: 0

Related Questions