Segue不会将数据传递到TableView

时间:2013-11-22 23:20:39

标签: ios iphone objective-c uitableview segue

我正在尝试将另一个名为BillTrackerViewController的视图控制器中的数组设置为位于主类MainViewController中的名为billtrackertablevalues的NSMutableArray的值。我打算在BillTrackerViewController中使用数组作为UITableView的数据。但是,当我尝试设置该数组,table_items和segue的值时,该表不显示任何内容。但是,billtrackertablevalues的值已正确填充,并且不为空。

这是我的MainViewController代码:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"toBillTracker"]) {
    PFQuery *query = [PFQuery queryWithClassName:@"Resolution"];
    [query whereKey:@"event" equalTo:event_name_label.text];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (PFObject *object in objects) {
                [billtrackertablevalues addObject:[NSString stringWithFormat:@"%@\nPro: %@\nCon: %@\nResult: %@",[object objectForKey:@"resolution_name"], [object objectForKey:@"resolution_pro_speaker"], [object objectForKey:@"resolution_con_speaker"], [object objectForKey:@"resolution_result_id"]]];
            }
            NSLog(@"%@", billtrackertablevalues);

        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

    BillTrackerViewController *controller=(BillTrackerViewController *)segue.destinationViewController;
    controller.table_items = [[NSMutableArray alloc] init];
    controller.table_items = billtrackertablevalues;
}
}

这是我的BillTrackerViewController代码:

#import "BillTrackerViewController.h"
#import "Parse/Parse.h"

@interface BillTrackerViewController ()

@end

@implementation BillTrackerViewController

@synthesize billView;
@synthesize refreshButton;

@synthesize table_items;

- (void)viewDidLoad
{
[super viewDidLoad];
[billView reloadData];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

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

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

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

- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}

NSString *cellText = table_items[indexPath.row];
cell.textLabel.text = cellText;
return cell;
}

@end

BillTrackerViewController上的表再次没有加载值,但我的初始数组,billtrackertablevalues被填充。

先谢谢你,伙计们!

2 个答案:

答案 0 :(得分:0)

我认为您需要移动实例化控制器的行,并将其属性设置为块方法内部:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"toBillTracker"]) {
        PFQuery *query = [PFQuery queryWithClassName:@"Resolution"];
        [query whereKey:@"event" equalTo:event_name_label.text];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (PFObject *object in objects) {
                [billtrackertablevalues addObject:[NSString stringWithFormat:@"%@\nPro: %@\nCon: %@\nResult: %@",[object objectForKey:@"resolution_name"], [object objectForKey:@"resolution_pro_speaker"], [object objectForKey:@"resolution_con_speaker"], [object objectForKey:@"resolution_result_id"]]];
            }
            NSLog(@"%@", billtrackertablevalues);
            BillTrackerViewController *controller=(BillTrackerViewController *)segue.destinationViewController;
            controller.table_items = billtrackertablevalues;

        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        }];
    }
}

编辑后:

仍然存在计时问题,因为segue在块返回之前发生,因此您应该覆盖BillTrackerViewController中table_items的setter。 segue仍将首先出现(您可能希望在您的视图中放置一个微调器,以便用户知道正在发生的事情),然后当块完成时,将调用setter:

-(void)setTable_items:(NSString *)table_items {
    _table_items = table_items;
    //update your table here.
}

答案 1 :(得分:0)

这个怎么样:

controller.table_items = [[NSMutableArray alloc] initWithArray:billtrackertablevalues];

此问题可能出在BillTrackerViewController本身