【问题标题】:iOS JSON Parsing to tableview, incorrect rowsiOS JSON解析到tableview,不正确的行
【发布时间】:2013-09-13 16:08:18
【问题描述】:

我正在开发一个 iOS 应用程序,我有一个从 MySQL 数据库填充的 tableview 控制器。当我单击原始视图时,会出现一个详细视图,其中显示数据库中的数据,例如主详细信息应用程序。问题如下: 1.我在iPhone模拟器上运行app时,app显示mysql表中的3条记录,没错。 2. 如果我点击第一行,什么都没有发生。 3. 如果我点击第二行或第三行,应用程序会打开详细视图,但数据与所选行(记录)不对应。 4.我点击detailview的返回按钮,app再次打开masterview。 5. 现在,如果我单击第一行,应用程序会打开详细视图,但如前所述,详细视图数据与主视图上的选定行(记录)不对应。 这里有我的视图控制器代码(主视图):

    //
    //  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

这里是detailview代码:

//
//  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

知道我做错了什么吗? 谢谢

【问题讨论】:

    标签: mysql ios json parsing


    【解决方案1】:
    - (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

    【讨论】:

    • 谢谢Dante,我找了很长时间的原因。没错。
    • 不客气,如果它解决了您的问题,您应该接受答案。
    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多