iOS JSON解析为tableview,行不正确

时间:2013-09-13 16:08:19

标签: mysql ios json parsing

我正在开发一个iOS应用程序,我有一个从MySQL数据库填充的tableview控制器。单击原始时,将显示详细视图,显示数据库中的数据,例如主详细信息应用程序。问题如下: 1.当我在iPhone模拟器上运行应用程序时,应用程序显示mysql表中的三条记录,这是正确的。 如果我点击第一行,则没有任何反应。 3.如果我单击第二行或第三行,应用程序将打开详细视图,但数据与所选行(记录)不对应。 4.我点击详细视图的后退按钮,应用程序再次打开主视图。 5.现在,如果我单击第一行,应用程序将打开详细信息视图,但如前所述,详细信息视图数据与主视图上的选定行(记录)不对应。 这里有我的viewcontroller代码(主视图):

    //
    //  ViewController.m

   //

#import "ViewController.h"
#import "DetailViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Areas";
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
    NSURL *url = [NSURL URLWithString:@""];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc]initWithRequest:request delegate:self];

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

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

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

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
    news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
    [mainTableView reloadData];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connect to either 3G or WiFi" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

}

-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(int)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return [news count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if(cell==nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
                }
    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"name"];
      cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"nombre"];
    return cell;
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.title =[[news objectAtIndex:indexPath.row]objectForKey:@"nombre"];
    detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailViewController animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这里是详细视图代码:

//
//  DetailViewController.m

//

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize newsArticle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    titleLabel.text = [newsArticle objectForKey:@"nombre"];
    timeLabel.text = [newsArticle objectForKey:@"name"];

    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

知道我做错了什么? 谢谢

1 个答案:

答案 0 :(得分:2)

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.title =[[news objectAtIndex:indexPath.row]objectForKey:@"nombre"];
    detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailViewController animated:YES];
}

您正在使用 didDeselectRow ,当取消选择某行时会调用该行,因此会出现奇怪的行为和错误的数据..您必须使用didSelectRow