如何从以前的tableview获取值

时间:2012-01-27 14:37:06

标签: objective-c uitableview

我有2个视图控制器。在第一篇文章中,我在tableview中选择了一个类别。在下一个视图中,我想显示我在上一个tableview中选择的那个类别中的所有产品。我用php文件从我的数据库中获取数据。

这是我的firstViewController.m'catVal'是我想给下一个ViewController的值。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KiesProduct *proView = [[KiesProduct alloc] initWithNibName:@"KiesProduct" bundle:nil];
    [self.navigationController pushViewController:proView animated:YES];
    NSDictionary *info = [json objectAtIndex:indexPath.row];
    NSString *catVal = [info objectForKey:@"Cat_naam"];
    [productVC fillArrayProducts:catVal];


}

在我的第二个视图控制器中,我有fillArayProducts函数。

-(void) fillArrayProducts:(NSString *)cat{
    NSLog(@"test");
    NSMutableString *postString = [NSMutableString stringWithString:kGETProducts];
    [postString appendString:[NSString stringWithFormat:@"?%@=%@",@"Pro_cat",cat]];
    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postString]];

    NSError *error;

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    arrayProducts = [[NSMutableArray alloc] init];
    for (int i=0; i<[json count]; i++) {
        NSDictionary *info = [json objectAtIndex:i];
        [arrayProducts addObject:[info objectForKey:@"Pro_naam"]];
    } 
    NSLog(@"%@",arrayProducts);  

}

我的secondViewController.m

#import <UIKit/UIKit.h>
#define kGETProducts @"http://localhost/getProducts.php"
@interface KiesProduct : UITableViewController{
    NSMutableArray *json;
    NSMutableArray *arrayProducts;
    NSURLConnection *postConnection; 
}
-(IBAction)add:(id)sender;
-(void) fillArrayProducts:(NSString *)cat;
@end

当我试试这个'[productVC fillArrayProducts:catVal];'在我的tableview DID选择行功能它不起作用。有人有个主意吗?

1 个答案:

答案 0 :(得分:0)

你正在调用你的第二个视图* proView,然后你在productVC上调用fillArrayProducts。改为

KiesProduct *proView = [[KiesProduct alloc] initWithNibName:@"KiesProduct" bundle:nil];
NSDictionary *info = [json objectAtIndex:indexPath.row];
NSString *catVal = [info objectForKey:@"Cat_naam"];
[proView fillArrayProducts:catVal];
[self.navigationController pushViewController:proView animated:YES];