2016-03-08 46 views
0

我的演示代碼很簡單:我希望在視圖控制器是目前從網絡加載,所以我必須從viewDidLoad中手動調用beginRefreshing顯示加載refreshControl在開始uirefreshcontrol不顯示時的tableview有許多行

#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 

,如果我只設置段的數量爲1(總行數是3),每個事物順利 enter image description here

但如果我改變部分的數目,以較大數目像10(總的行是30) ,刷新控制將不會顯示

enter image description here

是系統錯誤嗎?或者我使用刷新控制的方式是錯誤的?

+0

請張貼代碼沒有截圖的代碼。 – Leo

+0

@Leo OK,我已經發布了代碼:) – ximmyxiao

回答

1
實際上

其表示即使有10個部分,每個30行。 問題是它隱藏在頂部。 試試這個:

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]; 

,我不知道爲什麼你要顯示加載初始。它必須在下拉的用戶操作時觸發。然後你做頁面數據刷新並重新加載頁面。 你也應該考慮加入

-(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]; 
     }); 
    }); 
} 
+0

,因爲在真實的代碼中,我想從網絡加載當視圖控制器存在時,所以我想要在開始時看到刷新控制,所以我必須手動設置內容偏移來實現這一目標? – ximmyxiao

+0

你認爲這有點兒系統錯誤嗎? – ximmyxiao

+0

我不得不改變偏移量來實現這個,就像我提到的那樣。因爲UIRefreshControl和UITableView獨立工作。 –

0
remove this line 

self.tableView.tableFooterView = [[UIView的頁頭] initWithFrame:方法CGRectMake(0,0,1,1)];

,做這樣

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]; 
} 
+0

想要從網絡加載視圖控制器時,所以我必須調用beginRefreshing手動從viewdidload – ximmyxiao