1

我正在开发一个 iOS 应用程序,我有一个从 MySQL 数据库填充的 tableview 控制器。当我单击原始数据时,会出现一个详细视图,其中显示数据库中的数据,例如主详细信息应用程序。问题如下: 1.当我在iPhone模拟器上运行该应用程序时,该应用程序显示了mysql表中的三个记录,这是正确的。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

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

4

1 回答 1

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

于 2013-09-13T16:11:00.683 回答