iphone UItableview from xml



我有一个表视图,我想从xml填充信息。在viewdidload中,我告诉解析器开始解析。我将信息存储在NSMUtableArray中。

但是当我到达这个函数时它给了我一个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;
}

我得到一个SIGABRT错误,说"索引0超出空数组的界限",在行:

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

似乎它试图在解析完成之前将数据放入表中。我该如何避免这种情况?

感谢编辑:下面是进行解析的代码:
    - (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
{
    if ([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];
    }
}

//////这里的代码创建数组////////////

- (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]; 
}

您需要从您的XML文件中填充NSMutableArray,然后您可以将您的UITableViews 数据源连接到NSMutableArray .

查看 NSXMLParser Class Reference

我是对的-表试图从尚未填充的数组中填充自己(它的大小为0)。解决方案是一个肮脏的hack;用空格初始化它,然后使用replaceatindexwith,然后重新加载表。

Thanks for the help

最新更新