UITableView: NSMutableArray not appearing in table
I'm new to objective c and iPhone programming. I can't seem to figure out why no cells will fill when I run this code. The xml content displays in the console log and xcode displays no errors. Can any help?
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if(![elementName compare:@"Goal"]) {
tempElement = [[xmlGoal alloc] init];
} else if (![elementName compare:@"Title"]) {
currentAttribute = [NSMutableString string];
} else if (![elementName compare:@"Progress"]) {
currentAttribute = [NSMutableString string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI q开发者_如何学PythonualifiedName:(NSString *)qName
{
if (![elementName compare:@"Goal"]) {
[xmlElementObjects addObject:tempElement];
} else if (![elementName compare:@"Title"]) {
NSLog(@"The Title of this event is %@", currentAttribute);
[tempElement setTitled:currentAttribute];
} else if (![elementName compare:@"Progress"]) {
NSLog(@"The Progress of this event is %@", currentAttribute);
[tempElement setProgressed:currentAttribute];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (self.currentAttribute) {
[self.currentAttribute appendString:string];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [xmlElementObjects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [mtableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
I can't seem to figure out why no cells will fill when I run this code.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [xmlElementObjects count];
}
Is this method getting hit? If so, is xmlElementObjects
nil
? If not, is it returning 0 since there are no elements in the array? If so, is it because your XML parsing code didn't fill the array before the array is being used?
Another thing you need to check: Did you set the dataSource property of your tableview to the correct object? Other than that, check if your if(![elementName compare:@"Goal"])
is really 'hit' (either by NSLog()ing or putting a breakpoint there.
Another note, and unrelated, you should probably release tempElement
after adding it to the xmlElements
dictionary.
Set breakpoints in -tableView:numberOfRowsInSection:
and -tableView:cellForRowAtIndexPath:
to ensure that they're being called. As Alfons mentioned, it may be that your table view's datasource
property is not being set.
Some unrelated notes:
Why are you using the statement
![someString compare:someOtherString]
for comparison?NSString
has a built-in-isEqualToString:
method, so you can replace that statement with[someString isEqaulToString:someOtherString]
. Your results won't change, but your code will benefit from the added readability.In
-tableView:cellForRowAtIndexPath:
, you have the following code:UITableViewCell *cell = [mtableview dequeueReusableCellWithIdentifier:CellIdentifier];
This might cause problems if you keep using the instance variable (in this case,
mtableview
) here. The method has a table view as an argument, so usetableView
instead. That will ensure that you dequeue a cell from the table view that called this method.Are you developing for iPhone OS 3.0 or above? If so, the line
[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
should be replaced with
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
using whichever cell style you'd like, naturally.
Well xmlElementObjects
isn't a array of strings, it contains instances to some custom class you've created (I assume). Thus, when you say cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row];
, you're setting a NSString
to some unknown object.
Try cell.textLabel.text = [[xmlElementObjects objectAtIndex:indexPath.row] titled];
(I'm guessing the title name from the parsing code)
Objective C is very new to me. I am used to writing PHP which seems very easy compared to this so far. I set the datasource and also change compare to isequaltostring. This is what the console log states now:
2010-04-07 02:28:16.580 bucket list[6449:20b] The Title of this event is Test 1 10%
2010-04-07 02:28:16.581 bucket list[6449:20b] The Title of this event is Test 2 20%
2010-04-07 02:28:16.584 list[6449:20b] * -[xmlGoal isEqualToString:]: unrecognized selector sent to instance 0x3d36600 2010-04-07 02:28:16.585 list[6449:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[xmlGoal isEqualToString:]: unrecognized selector sent to instance 0x3d36600'
I appreciate all the help!
精彩评论