UITefresh for UITableView无法运行iOS 10

时间:2017-02-08 11:51:41

标签: ios objective-c iphone uitableview

我正在尝试使用UIRefresh来重新加载带有最新数据的UITableView。

    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self
                            action:@selector(myRefresh)
                  forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:self.refreshControl];

- (void)myRefresh
{
  //Empty array which hold old data.
 //calling API to get fresh data 
}

-(void)delegatefromAPI
{ 
    //array will be reloaded with data from DB
    [self.tableView reloadtable];
    // End the refreshing
    if (self.refreshControl) {
        [self.refreshControl endRefreshing];
    }
}

但问题是在API调用获取数据之前,此时会调用cellForRowAtIndexPath(自动)数组加载每个空单元格为空。
但是我在获取数据然后调用[self.tableView reloadtable];后调用"[self.refreshControl endRefreshing]"。 但是在delegatefromAPI中[self.tableView reloadtable]之前调用了cellForRowAtIndexPath。 我哪里错了.....

1 个答案:

答案 0 :(得分:0)

 self.refreshControl = [[UIRefreshControl alloc] init];
 self.refreshControl.tintColor = YOURCOLOR_FOR_REFRESHCONTROL;
 [self.refreshControl addTarget:self action:@selector(fetchContactList) forControlEvents:UIControlEventValueChanged];
 if #available(iOS 10.0, *) {
       self.YOURTABLE.refreshControl = refreshControl
 } else {
       [self.YOURTABLE addSubview:self.refreshControl];
 }



-(void)fetchContactList{
       // your API call inside that stop animation
       [self.refreshControl endRefreshing];
       [self.YOURTABLE reloadData];
}

#pragma mark - UITableview delegate method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return YOURARRAY.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"YOURCELLIDENTIFIER";
    RegisteredContactTVCell *cell = (RegisteredContactTVCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
   cell.lblName.text = @"VALUE";
   return cell;
}

输出:

CHECK