0

我有一个 MainViewController 和一个UITableViewController. 我调用UITableViewController使用自定义段添加 MainViewConroller 的子视图。当我UITableViewController独立测试时,一切正常并且数据已UITableView成功填充。当我从 MainViewController 调用时,数据没有被填充到UITableView. 我的方法有什么问题?这是通过以下代码完成的。

CustomTabBarSegue.m

#import "CustomTabBarSegue.h"
#import "MainViewController.h"

@implementation CustomTabBarSegue

-(void)perform{
    MainViewController *rootVC = (MainViewController *)self.sourceViewController;
    UIViewController *dst = (UIViewController *)self.destinationViewController;
    for(UIView *view in rootVC.placeholder.subviews){
        [view removeFromSuperview];
    }
    rootVC.currentViewController = dst;
    [rootVC.placeholder addSubview:dst.view];
}

ListingViewConroller.m

#import "ListingViewController.h"
#import "AFNetworking.h"
#import "Constants.h"

@interface ListingViewController ()
@end

@implementation ListingViewController

@synthesize jobs;
@synthesize jobDictionary;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self makeOperationRequest];
}

-(void)makeOperationRequest
{
    NSURL *url = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"http://localhost/api/jobs"]];

    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject) {
        jobDictionary = (NSDictionary *)responseObject;
        jobs = [jobDictionary objectForKey:@"jobs"];
        [self.tableView reloadData];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error:%@,%@",error,error.userInfo);
    }];
    [operation start];
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return jobs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];   
    cell.textLabel.text = [[jobs objectAtIndex:indexPath.row] objectForKey:@"CustomerName"];
    cell.detailTextLabel.text = [[jobs objectAtIndex:indexPath.row] objectForKey:@"Address"];
    return cell;
}
4

1 回答 1

0

将表视图添加为子视图后尝试重新加载表视图。

[tableView reloadData];

愿它能解决你的问题。

于 2013-08-09T10:22:49.253 回答