Iphone Tableview上载NSMutableArray项的代码如何?

Iphone Tableview上载NSMutableArray项的代码如何?,iphone,Iphone,大家好,根据下面的代码,我正在尝试将NSMutableArray项上载到tableview,但它显示了如下问题 ContactTesting[1040:f803]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSMutableArray count]:方法发送到未初始化的可变数组对象” **第一次抛出调用堆栈: (0x13bc052 0x154dd0a 0x13a820b 0x247b 0x1edf2b 0x1f0722 0xa07c7 0

大家好,根据下面的代码,我正在尝试将
NSMutableArray
项上载到tableview,但它显示了如下问题

ContactTesting[1040:f803]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSMutableArray count]:方法发送到未初始化的可变数组对象” **第一次抛出调用堆栈: (0x13bc052 0x154dd0a 0x13a820b 0x247b 0x1edf2b 0x1f0722 0xa07c7 0xa02c1 0xa328c 0xa7783 0x52322 0x13bde72 0x1d6692d 0x1d70827 0x1cf6fa7 0x1cf8ea6 0x1d8430c 0x124c6 0x12bd6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x139C5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x1bc8 0x1B025)

基本上我得到的是
NSMutableArray*array
项目如下

2012-02-29 14:24:20.246 ContactTesting[564:f803] name is->>>>>>> : (
"\n        Enrico Gaetani",
"\n        J\U00e9r\U00f4me Tredan",
"\n        Mike Tsalidis",
"\n        Dana Sugarman",
"\n        Necip Ozyucel",
"\n        Goksel Topbas",
"\n        Nizar Abdul-Baki",
"\n        Johannes Kanis",
"\n        Kara Westhusing",
"\n        Andreas Erlacher",
"\n        Rudy Van Hoe",
"\n        Lars Nygaard",
"\n        Juha Karppinen",
"\n        Ronan Geraghty",
"\n        Luca Venturelli",
"\n        Michel N'Guettia",
"\n        John Henrik Andersen",
"\n        Jos\U00e9 Grilo",
"\n        Luis Miguel Garcia Oliva",
"\n        Andre Henriksson",
"\n        Marc Holitscher",
"\n        Lucas Searle",
"\n        Ansgar Heinen",
"\n        Vasily Malanin",
"\n        Marek Zamlynski",
"\n        Petr Vasa",
"\n        Peter Budai",
"\n        Kostyantyn Khrushchelov",
"\n        Nicholas Molfetas",
"\n        Bogdan Constantinescu",
"\n        Peter Jirsak",
"\n        Marko Drev",
"\n        Petra Cicek",
"\n        Dragana Jovic (E-Search)",
"\n        Linas Peciura",
"\n        Tsvetomir Mitev",
"\n        Renat Begaidar",
"\n        Pauline Maillard",
"\n        Mark Chaban",
"\n        Dayana Orlan-Nevo",
"\n        Neveen Nassif",
"\n        Francois Smith",
"\n        Jeroen Unger",
"\n        Kris Vandermeulen",
"\n        Claus Petersen",
"\n        Riku Reimaa",
但是,当我使用tableView方法时,它退出了功能,因此如何解决问题,给出解决方案,这对我在iphone中来说太紧迫了

- (void)viewDidLoad
{
   [super viewDidLoad];


    [self testing];

    array=[NSMutableArray alloc];
    myTableView.delegate=self;
    myTableView.dataSource=self;
    myTableView.separatorStyle=  UITableViewCellSeparatorStyleNone;

    myTableView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"list.png"]];
    myTableView.scrollEnabled=YES;


// Do any additional setup after loading the view, typically from a nib.
}







- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1;   
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return [array count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *CellIdentifier = @"Cell";
//here you check for PreCreated cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

//Fill the cells...  
cell.textLabel.text = [array objectAtIndex: indexPath.row];
//yourMutableArray is Array 
return cell;
}

-(void)testing{
//  NSLog(@">>>>>>>>>>>>233333");

NSString *jobSearchUrlString = [NSString stringWithFormat:@"http://infra2appsmobile.cloudapp.net/Infra2Apps.svc/Contacts"];
//NSLog(@"url for new articles is = %@",jobSearchUrlString);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:jobSearchUrlString]];   

NSURLConnection *theconnection =  [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theconnection) {
    RoutData = [[NSMutableData alloc] init];
}




}



-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{   

[RoutData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[RoutData appendData:data];

NSString *thexml = [[NSString alloc] initWithData:RoutData encoding:NSUTF8StringEncoding];

NSDictionary *dictXML= [XMLReader dictionaryForXMLString:thexml error:nil];

NSMutableArray *arrStation = [[dictXML objectForKey:@"feed"] objectForKey:@"entry"] ;//this would return the array of station dictionaries



//    for (int i=0; i<[arrStation count]; i++) {
//        NSLog(@"--->>>ContactNameis:<<<---- %@",[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:ContactName"] objectForKey:@"text"]);



    array=[[NSMutableArray alloc]init];
    for (int i=0; i<[arrStation count]; i++){
        NSString *str=[[[[[arrStation objectAtIndex:i] objectForKey:@"content"]   objectForKey:@"m:properties"] objectForKey:@"d:ContactName"] objectForKey:@"text"];
      //  NSlog(@"name is : %@",str);
        //NSLog(@"name is->>>>>>> : %@",str);
        [array addObject:str];
        NSLog(@"name is->>>>>>> : %@",array);

               }



    // NSString *str=[[arrStation objectAtIndex:i ] objectForKey:@"d:ContactName"];
    //NSLog(@"8********************8%@",[[arrStation objectAtIndex:i ] objectForKey:@"d:ContactName"]);
    //NSLog(@"--->>>id is ::<<<---- %@",[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:Id"] objectForKey:@"text"]);
    //        
    // NSLog(@"--->>>Emails ::<<<---- %@",[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:Email"] objectForKey:@"text"]);
//        
//NSLog(@"--------->>>>>>>>>>><<<<<<<<<<<%@",arrStation);

 }
}
-(void)viewDidLoad
{
[超级视图下载];
[自我测试];
数组=[NSMUTABLEARRY alloc];
myTableView.delegate=self;
myTableView.dataSource=self;
myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
myTableView.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage ImageName:@“list.png”];
myTableView.scrollEnabled=是;
//加载视图后,通常从nib执行任何其他设置。
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
返回1;
}
-(NSInteger)表格视图:(UITableView*)表格行数节:(NSInteger)节{
返回[数组计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
//在这里,您可以检查预处理的细胞。
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
//填充单元格。。。
cell.textlab.text=[array objectAtIndex:indexath.row];
//yourMutableArray是数组
返回单元;
}
-(无效)测试{
//NSLog(@“>>>>>>>>233333”);
NSString*jobSearchUrlString=[NSString stringWithFormat:@]http://infra2appsmobile.cloudapp.net/Infra2Apps.svc/Contacts"];
//NSLog(@“新文章的url为%@”,jobSearchUrlString);
NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:jobSearchUrlString]];
NSURLConnection*连接=[[NSURLConnection alloc]initWithRequest:request委托:self];
if(连接){
RoutData=[[NSMutableData alloc]init];
}
}
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{   
[RoutData setLength:0];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[路由数据:数据];
NSString*thexml=[[NSString alloc]initWithData:RoutData编码:NSUTF8StringEncoding];
NSDictionary*dictXML=[XMLReader dictionaryForXMLString:thexml错误:nil];
NSMutableArray*arrStation=[[dictXML objectForKey:@“feed”]objectForKey:@“entry”];//这将返回站点字典数组

//对于(int i=0;i,在接收所有数据后,重新加载tableView

// array=[[NSMutableArray alloc]init];   remove this 
for (int i=0; i<[arrStation count]; i++){
     NSString *str=[[[[[arrStation objectAtIndex:i] objectForKey:@"content"]   objectForKey:@"m:properties"] objectForKey:@"d:ContactName"] objectForKey:@"text"];
     // NSlog(@"name is : %@",str);
     // NSLog(@"name is->>>>>>> : %@",str);
     [array addObject:str];
     NSLog(@"name is->>>>>>> : %@",array);
     [myTableView reloadData];
}
//array=[[NSMutableArray alloc]init];删除此

对于(int i=0;i,在接收所有数据后,重新加载tableView

// array=[[NSMutableArray alloc]init];   remove this 
for (int i=0; i<[arrStation count]; i++){
     NSString *str=[[[[[arrStation objectAtIndex:i] objectForKey:@"content"]   objectForKey:@"m:properties"] objectForKey:@"d:ContactName"] objectForKey:@"text"];
     // NSlog(@"name is : %@",str);
     // NSLog(@"name is->>>>>>> : %@",str);
     [array addObject:str];
     NSLog(@"name is->>>>>>> : %@",array);
     [myTableView reloadData];
}
//array=[[NSMutableArray alloc]init];删除此

对于(int i=0;i您在
viewdiload
方法中最初分配的
array
上缺少了
init
,因为在那之后不久,tableView将在项目从网络下载之前请求第0节中的项目计数。它将尝试处理未初始化的
NSMutableArray
,这正是例外情况所说的


下载内容并完成解析后,填充数组,重新加载节或整个表。

viewDidLoad
方法中初始分配的
array
上缺少
init
,因为在这之后不久,tableView将要求计算节0中的项目数,肯定是在项目开始之前从网络下载。它将尝试在未初始化的
NSMutableArray
上工作,这正是异常所说的


下载内容并完成解析、填充数组后,重新加载节或整个表。

viewDidLoad方法中执行此操作

array = [[NSMutableArray alloc] init];

viewDidLoad方法中执行此操作

array = [[NSMutableArray alloc] init];