views:

53

answers:

3

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

}

A: 
  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?

lostInTransit
I followed your first section but it still caused errors. I have NSLogs in numberOfRows and cellForRow but none of those get called when it errors.
aahrens
A: 

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?

Ivan Vučica
Yes I'm certain. I want numberOfSectionsInTableView to return [newsItems count] so if count was 10. I would have 10 sections. For each of those sections numberOfRowsInSection will just return 1 because I only want to display 1 newsItem in each section.I changed int length = [newsItems count] and return length still gave me an error
aahrens
"For each of those sections numberOfRowsInSection will just return 1" <== You're using future tense, but despite that, I presume you did implement `numberOfRowsInSection`? Just checking, despite it being too obvious.
Ivan Vučica
+1  A: 

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 )

sugar
I followed that suggestion but it still manages to error and my UITableView won't be loaded.
aahrens
what error did you got ?
sugar
When I debugged the numberOfSectionsInTableView method it was saying that [newsItems count] was zero. Which would explain why the UITableView wasn't being populated.I think I need to retain my newsItems array but not sure how to do that.
aahrens
If your - [newsItems count] is zero. Then you forgot to alloc your array.
sugar