开发者

iphone UItableview from xml

I have a table view that i want to populate with information from xml. In viewdidload I tell the parser to start parsing. I store the information in an NSMUtableArray.

However when I get to this function it gives me a SIGABRT

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    NSString *celltext = [profileItems objectAtIndex:indexPath.row]; //SGABRT here

    NSString *celltitle = [profileFieldNames objectAtIndex:indexPath.row];

    cell.detailTextLabel.text = celltext;

    cell.textLabel.text = celltitle;

    return cell;
}

I get a SIGABRT error saying "index 0 beyond bounds for empty array'" at the line:

NSString *celltext = [profileItems objectAtIndex:indexPath.row]; //SGABRT here

It seems that it is trying to put the data in the table before the parsing is complete. How do I avoid this?

Thanks

EDIT: Here is the code that does the parsing:

    - (void)parser:(NSXMLParser *)parser
   didStartElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI
     qualifiedName:(NSString *)qualifiedName
        attributes:(NSDictionary *)attributeDict
{
    currentElement = [[elementName copy] autorelease];
    if ([elementName isEqualToString:@"fullname"]) 
    {
        tfullname = [[NSMutableString alloc] init];
    }

    if ([elementName isEqualToString:@"DOB"])
    {
        tDOB = [[NSMutableString alloc] init]; 
    }

    if ([elementName isEqualToString:@"accommodation"])
    {
        taccommodation = [[NSMutableString alloc] init]; 
    }

    if ([elementName isEqualToString:@"nationality"])
    {
        tnationality = [[NSMutableString alloc] init]; 
    }

    if ([elementName isEqualToString:@"subject"])
    {
        tstudying= [[NSMutableString alloc] init]; 
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    i开发者_开发知识库f ([currentElement isEqualToString:@"fullname"])
    {
        [tfullname appendString:string];
    }

    if ([currentElement isEqualToString:@"DOB"])
    {
        [tDOB appendString:string];
    }

    if ([currentElement isEqualToString:@"accommodation"])
    {
        [taccommodation appendString:string];
    }

    if ([currentElement isEqualToString:@"nationality"])
    {
        [tnationality appendString:string];
    }

    if ([currentElement isEqualToString:@"subject"])
    {
        [tstudying appendString:string];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"fullname"]) 
    {     
        namelabelstring = tfullname;
        NSLog(@"second attemp your name is %@", namelabelstring);
    }

    if ([elementName isEqualToString:@"DOB"]) 
    {
        [profileItems addObject:tDOB];
    }

    if ([elementName isEqualToString:@"accommodation"])
    {
        [profileItems addObject:taccommodation];
    }

    if ([elementName isEqualToString:@"nationality"])
    {
        [profileItems addObject:tnationality];    }

    if ([elementName isEqualToString:@"subject"])
    {
        [profileItems addObject:tstudying];
    }
}

//////Here is the code where the arrays are created////////////

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    profileFieldNames = [[NSMutableArray alloc] init ];
    profileItems = [[NSMutableArray alloc] init];

    [profileFieldNames addObject:@"Date of Birth:"];
    [profileFieldNames addObject:@"Nationality:"];
    [profileFieldNames addObject:@"Accommodation:"];
    [profileFieldNames addObject:@"Studying:"];

    //[[self tableView] reloadData];

    [self loadmemberprofiledata];

    [[NSBundle mainBundle] loadNibNamed:@"ProfileViewHeader" owner:self options:nil];

    [self setHeaderView]; 
}


You need to populate an NSMutableArray from your XML file, then you can hook your UITableViews DataSource to the NSMutableArray.

Look into NSXMLParser Class Reference.


I was right - the table was trying to fill itself from an array that had not yet been filled (it was of 0 size). The solution a dirty hack; initialize it with spaces then use replaceatindexwith and then reload the table.

Thanks for the help

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜