ximmyxiao
ximmyxiao

Reputation: 2811

uirefreshcontrol not showing when tableview has many rows

my demo code is very simple:i want to load from network when the view controller is present , so i have to call beginRefreshing manually from viewdidload to show the loading refreshControl at beginning

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) UIRefreshControl* control;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

    self.control = [UIRefreshControl new];
    [self.tableView addSubview:self.control];
    [self.tableView reloadData];
    [self.control beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];


}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}
@end

and If i just set number of sections to 1(total rows is 3), every things goes well enter image description here

but if i change number of sections to a bigger number like 10(total rows is 30) , the refresh control will not show

enter image description here

is it a system bug ? or the way i use refresh control is wrong ?

Upvotes: 0

Views: 931

Answers (2)

Amit Tandel
Amit Tandel

Reputation: 883

Actually its showing even with 10 sections with 30 rows each. The problem is its getting hidden at top. Try this:

self.control = [UIRefreshControl new];
[self.tableView addSubview:self.control];
[self.tableView reloadData];
[self.control beginRefreshing];
[self.tableView setContentOffset:CGPointMake(0, -self.tableView.contentInset.top) animated:true];

And i dont know why do you want to show loading initial. It has to be triggered on user action of pulldown. Then you do page data refresh and reload the page. You should also consider adding

-(void)setUpRefreshControl
{
    self.control = [UIRefreshControl new];
    [self.tableView addSubview:self.control];
    [self.tableView reloadData];
//    [self.control beginRefreshing];
    [self.tableView setContentOffset:CGPointMake(0, -self.tableView.contentInset.top) animated:true];
    [ self.control addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];

}

- (void)refresh {
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        //Background Thread
        //Do data refresh task here like getting new data for UI.
        sleep(1);
        dispatch_async(dispatch_get_main_queue(), ^(void){
            [self.control endRefreshing];
            [self.tableView reloadData];
        });
    });
}

Upvotes: 1

Saood
Saood

Reputation: 283

remove this line

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

and do like this

self.control  =  [UIRefreshControl new];
[self.tableView addSubview: self.control];
[ self.control addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];

- (void)refresh {
    //refresh your data
    [self.control endRefreshing];
    [self.tableView reloadData];
}

Upvotes: 0

Related Questions