2012-09-12 123 views
0

我正在使用uitableview來顯示json解析的數據。解析的數據存儲在數組中,並且數組列表100被分配給uitableview。但它摔碎在objectAtIndex 在{for循環}它顯示崩潰報告(終止應用程序,由於未捕獲的異常'NSInvalidArgumentException)

終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',理由是:「 - [NSMutableArray的insertObject:atIndex:]:嘗試插入零在0'對象*

普萊舍幫助我

- (void)viewDidLoad 
{ 
    self.responseData = [NSMutableData data]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:openexchangeURl]]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [super viewDidLoad]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [responseData setLength:0]; 
} 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
} 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [connection release]; 
    self.responseData = nil; 
} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [connection release]; 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    self.responseData = nil; 

    values = [responseString JSONValue]; 
    array = [[NSMutableArray alloc] init]; 
    NSMutableArray *arrTitle = [[NSMutableArray alloc] init]; 
    NSMutableArray *arrValues = [[NSMutableArray alloc] init]; 
    array =[values valueForKey:@"rates"]; 

    NSLog(@"array values:--> %@",array); 
// NSLog(@"values:--> %@",values); 
// NSLog(@"Particular values:--> %@",[[values valueForKey:@"rates"] valueForKey:@"AED"]); 

    tempDict1 = (NSMutableDictionary *)array;    
    NSArray *arr;// =[[NSArray alloc]init]; 
    arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"]; 
    NSLog(@"arr-->%@",arr); 
    NSString *subStar = @"="; 
    [arrTitle removeAllObjects]; 
    [arrValues removeAllObjects]; 

    for (int i=0; i<[arr count]-1; i++) 
    { 
     [arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]]; 
     [arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]]; 
     NSLog(@"arrTitle is:--> %@",arrTitle); 
    } 

    tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0]; 
    array = [values valueForKey:@"rates"]; 
    NSLog(@"tempDict--%@",[tempDict1 objectForKey:@"AED"]); 

    [array retain]; 
    [tbl_withData reloadData]; 

} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"array-->%@",array); 
    return [array count]; 

} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.adjustsFontSizeToFitWidth = YES; 
     cell.textLabel.font = [UIFont systemFontOfSize:8]; 
     cell.textLabel.numberOfLines = 4; 

    } 

// NSLog(@"data is like:--> %@",array); 
// cell.textLabel.text= [NSString stringWithFormat:@"%@",[array objectAtIndex:intIndexPath]]; 
    cell.textLabel.text =[array objectAtIndex:intIndexPath]; 

    return cell; 
} 
+0

您的代碼工作正常....是什麼問題? – Rajneesh071

+0

在單個可用視圖單元 – anilkumar07

+0

中顯示的所有抓取數據(列表100)什麼是[values valueForKey:@「rates」]的outut。 – Rajneesh071

回答

0

還有就是你的數組中只有一個項目,在這裏你剛纔添加的一個對象:

array = [[NSMutableArray alloc] init]; 
[array addObject:[values valueForKey:@"rates"]]; 

您應該將從[values valueForKey:@"rates"]獲得的值分配給數組變量。還要使數組變量成爲保留該值的屬性。

@property (nonatomic, strong) NSArray *array; 

然後將其分配給屬性:

self.array = [values valueForKey:@"rates"]; 

而且將單元格​​的所有造型到這裏,如果你創建新的細胞。這會加快速度,因爲你不會每次都改變單元格的樣式。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
     cell.textLabel.adjustsFontSizeToFitWidth = YES; 
     cell.textLabel.font = [UIFont systemFontOfSize:8]; 
     cell.textLabel.numberOfLines = 4; 

    } 
    NSLog(@"data is like:--> %@",array); 
    // NSString *cellValue =[array objectAtIndex:indexPath.row]; 
    // cell.textLabel.text = cellValue; 

    cell.textLabel.text= [NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]]; 

    return cell; 
} 
+0

它的崩潰我的程序可以üplz幫我從這裏 – anilkumar07

+0

除非你能提供崩潰日誌。 – rckoenes

+0

我更新了我的問題崩潰日誌 – anilkumar07

0

以正確的格式從服務器獲取數據...在您的情況下,您的數據位於一個數組中。所以使它在不同的不同陣列..

{"reply":["AED = 3.673188","AFN = 48.5725","","","",""]} 

,或者你可以分析你的反應和單一單數據保存到另一個數組...

+0

{AED =「3.672563」; AFN =「48.723751」; ALL =「107.146666」; AMD =「411.284994」; ANG =「1.78035」; AOA =「95。394043「; ARS =」4.6604「; .....} – anilkumar07

+0

所以現在要打印什麼......只是數字(3.672563)或在您的行中完成(AED =」3.672563「) – Rajneesh071

+0

我想打印Aed =「3.672563」 – anilkumar07

相關問題