self.tableView reloadData无法正常工作

时间:2014-08-19 08:52:09

标签: ios objective-c uitableview reloaddata

[self.tableView reloadData]在调用时未更新numberOfRowsInSection。 在viewDidLoad

中以编程方式设置委托和数据源

另外,即使我在普通的UIViewController中使用tableView,也要输入UIRefreshControl

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView addSubview:refreshControl];
}

刷新控件调用位置对象以获取位置。当位置对象收到位置时,自我会收到通知。通知调用选择器getLocation以使用位置作为参数进行API调用

- (void)refresh:(UIRefreshControl *)refreshControl {

    [self.loadTableArray removeAllObjects];
    LPLocationManager *locationObject = [LPLocationManager sharedManager];
    [locationObject.locationManager startUpdatingLocation];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(getLocation:) name:@"locationNotification" object:nil];
    [refreshControl endRefreshing];    

}

发送到API请求对象的GET请求参数和加载mutableArray的self上的请求对象调用方法。 MutableArray被复制到NSArray并调用[self.tableView reloadData]但numberOfRowsInSection不会更新。

-(void)getLocation:(NSNotification*)notifications{

    self.latitude = notifications.userInfo[kSetLat];
    self.longitude = notifications.userInfo[kSetLong];
    self.requestObject = [[LPRequestObject alloc]init];
    [self.requestObject getLocationMediaWithLat:self.latitude andLong:self.longitude];

}


-(void)updateMutableArray:(NSArray*)array{

    self.loadTableArray = [[NSMutableArray alloc]init];
    for (NSDictionary *dictionary in array) {
        [self.loadTableArray addObject:dictionary];
    }
    self.loadTableArrayCopy = [self.loadTableArray copy];
    [self.tableView reloadData];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [self.loadTableArrayCopy count];

}

tableView被添加到自定义容器视图中的子视图中,其中包含在mainContentVC中实现的代码

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.locationViewController = [[LPLocationViewController alloc]init];
    self.locationViewController.view.frame = self.view.bounds;

    self.currentViewController = self.locationViewController;
    [self addChildViewController:self.currentViewController];
    [self.currentViewController didMoveToParentViewController:self];
    [self.containerView addSubview:self.currentViewController.view];

    self.diaryViewController = [[LPDiaryViewController alloc]init];
    self.diaryViewController.view.frame = self.view.bounds;

    self.settingsController = [[LPSettingsViewController alloc]init];
    self.settingsController.view.frame = self.view.bounds;

    if ((self.currentViewController = self.locationViewController)) {
        self.locationButton.enabled = NO;
    }

}

storyboard with container view

0 个答案:

没有答案
相关问题