0

NSMutablearray と JSON 解析に問題があります。それで、私は何をしていますか?JSON からの解析を行い、TableViewCell に送信します。私は自分のコードを持っています:

時間:

{
    NSMutableData *webdata;

    NSURLConnection *connection;

    NSMutableArray *array;

    NSMutableArray *array2;

NSTimer *timer;
}

母:

{
[super ViewDidload]

    [[self tableTrack] setDelegate:self];
    [[self tableTrack] setDataSource:self];
    array = [[NSMutableArray alloc] init];
    array2 = [[NSMutableArray alloc] init];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(plistGet) userInfo:nil repeats:YES];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webdata setLength:0];
}

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

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error load");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
    NSDictionary *playlist =[allDataDictionary objectForKey:@"playlist"];

    for (NSDictionary *diction in playlist) {
        NSDictionary *artist = [diction objectForKey:@"artist"];
        NSDictionary *song = [diction objectForKey:@"song"];
        NSString *name = [artist objectForKey:@"name"];
        NSString *namesong = [song objectForKey:@"name"];
        [array addObject:name];
        [array2 addObject:namesong];
    }
    [[self tableTrack]reloadData];
}

-(void)plistGet {


    [array removeAllObjects];
    [array2 removeAllObjects];
    NSURL *url = [NSURL URLWithString:@"http://plist.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        webdata = [[NSMutableData alloc]init];
    }

}


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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
//    return [array2 count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(! cell)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
    }

    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];

    cell.detailTextLabel.text = [array objectAtIndex:indexPath.row];

    cell.textLabel.textColor = [UIColor colorWithRed:(50/255.0) green:(200/255.0) blue:(255/255.0) alpha:1];


    [tableTrack setBackgroundView:nil];

    tableTrack.backgroundColor = [UIColor clearColor];

   cell.detailTextLabel.font=[UIFont fontWithName: @"Helvetica" size:11];

    UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 9.0 ];
    cell.textLabel.font  = myFont;

    self.tableTrack.separatorStyle = UITableViewCellSeparatorStyleNone;

   UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
    separatorView.layer.borderColor = [UIColor darkGrayColor].CGColor;
    separatorView.layer.borderWidth = 0.5;
    [cell.contentView addSubview:separatorView];

    return cell;

}

すべて非常にうまく機能しますが、5 ~ 3 分後にエラーが発生し、アプリがクラッシュしました。

エラー:

2013-06-21 12:32:41.502 EuropaPlusUA[651:707] *キャッチされない例外 'NSRangeException' が原因でアプリを終了します。 stack: (0x353f188f 0x37798259 0x3533a9db 0xfa9d5 0x32e85efb 0x32e84fd9 0x32e84763 0x32e28f37 0x353501fb 0x32220aa5 0x322206bd 0x32224843 0x3222457f 0x3221c4b9 0x353c5b1b 0x353c3d57 0x353c40b1 0x353474a5 0x3534736d 0x36fe3439 0x32e53cd5 0xf8843 0xf87d0) terminate called throwing an exception

それは何ですか?助けてください。

4

3 に答える 3

1

あなたの問題は、おそらくplistgetその3〜5分後に別の方法を実行することです. そのメソッドは、配列からすべてのオブジェクトを破棄します。データがロードされている間、および配列をクリアしている間、テーブルのデータソースは空ですが、インデックス 0 からこのオブジェクトを取得しようとしています。これがクラッシュが発生する理由です。

ただし、解決策は簡単です。removeAllObjects メソッドをまったく呼び出さないでください。取得する内容で配列を置き換えるだけです。

サーバーからデータを取得した瞬間に行う必要があるメカニズムを置き換えます。

NSMutableArray *tempDataArray = [[NSMutableArray alloc] init];
//put retrieved data from your web call in the tempDataArray
array = tempDataArray;
于 2013-06-21T09:57:13.727 に答える
0

objectAtIndex だけがここにあると思います。

cell.detailTextLabel.text = [array objectAtIndex:indexPath.row];

plistgetによって配列が空になった可能性が高いようです

[array removeAllObjects];
于 2013-06-21T09:56:38.377 に答える