Objective c 从xml查看iphone UItableview
我有一个表视图,我想用xml中的信息填充它。在viewdidload中,我告诉解析器开始解析。我将信息存储在NSMUtableArray中 然而,当我使用这个函数时,它给了我一个SIGABRTObjective c 从xml查看iphone UItableview,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我有一个表视图,我想用xml中的信息填充它。在viewdidload中,我告诉解析器开始解析。我将信息存储在NSMUtableArray中 然而,当我使用这个函数时,它给了我一个SIGABRT - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";
- (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
查看。我是对的-表试图从尚未填充的数组(大小为0)中填充自身。解决方案是肮脏的黑客;使用空格初始化它,然后使用replaceAtineXwith,然后重新加载表
谢谢你的帮助似乎信息不够。尝试将NSLog放在记录profileItems计数以及indexPath.row的日志之上。这可能有助于了解有多少项目可用,以及需要什么索引。另外,您说数组可能没有填充-它是否正在填充bg中的xml异步?这是什么代码?这将有助于了解您是如何填充profileItems和ProfileFieldName的。我怀疑其中至少有一个实际上是空的。是的,这是一个异步连接…什么是bg?嘿,如果我不使用值填充表,则会填充NSMutableArray,最终会填充数组。您确定您的解析正在向数组中添加对象吗?您应该
NSLog
profileItems在didEndElement
结尾处同意。顺便问一下,解析在哪里调用?(这反过来会导致回调被调用)