2012-10-01 4 views
13

UIRefreshControl을 내 headerView 위에 이동하거나 최소한 contentInset과 함께 작동 시키려고합니다. 누구나 그것을 사용하는 방법을 알고 계십니까?UITableView 및 UIRefreshControl

TableView 내에서 스크롤 할 때 멋진 배경을 사용하기 위해 headerView를 사용했습니다. 나는 스크롤 가능한 배경을 갖고 싶었다.

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
// Set up the edit and add buttons. 

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
self.tableView.backgroundColor = [UIColor clearColor]; 

[self setWantsFullScreenLayout:YES]; 

self.tableView.contentInset = UIEdgeInsetsMake(-420, 0, -420, 0); 

UIImageView *top = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top.jpg"]]; 
self.tableView.tableHeaderView = top; 

UIImageView *bottom = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottom.jpg"]]; 
self.tableView.tableFooterView = bottom; 

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"settingsIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(showSettings)]; 
self.navigationItem.leftBarButtonItem = leftButton; 

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addList)]; 
self.navigationItem.rightBarButtonItem = addButton; 

//Refresh Controls 
self.refreshControl = [[UIRefreshControl alloc] init]; 

[self.refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged]; 
} 

답변

32

나는 당신의 의도가 contentInset의 물건이 무엇인지 정말 모르겠어요,하지만 jQuery과에 UIRefreshControl 추가의 측면에서, 그것은 수행 할 수 있습니다. UIRefreshControl은 실제로 UITableViewController와 함께 사용하기위한 것이지만, UITableView에 서브 뷰로 추가하면 마술처럼 작동합니다. 이것은 문서화되지 않은 동작이며, 다른 iOS 릴리스에서는 지원되지 않을 수 있지만, 비공개 API를 사용하지 않으므로 유효합니다.

- (void)viewDidLoad 
{ 
    ... 
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; 
    [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged]; 
    [self.myTableView addSubview:refreshControl]; 
} 

- (void)handleRefresh:(id)sender 
{ 
    // do your refresh here... 
} 

신용도는 @Keller for noticing this입니다.

+3

내부 UIViewController 당신은 self.refreshControl이 없으므로 완전히 잘못되었습니다. lensovet – Godfather

+0

위대한 조언 ... thx – paiego

+1

이 tableview는 titleview (적어도 iOS7에서)가 있으면 몇 가지 문제가 있습니다. 새로 고침하는 동안 아래로 스크롤하면 표 섹션이 제목보기의 높이만큼 오프셋되어 모두 벗어납니다. – AlBeebe

10

@에 첼론의 답변이 있지만 그 중 한 가지 사소한 제안이 있습니다. 새로 고침 컨트롤을 @property로 추가하면 나중에 액세스 할 수 있습니다. YourViewController.h에서

@property (nonatomic, strong) UIRefreshControl *refreshControl; 

YourViewController.m

-(void) viewDidLoad { 
    self.refreshControl = [[UIRefreshControl alloc] init]; 
    [self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged]; 
    [self.tableView addSubview:self.refreshControl]; //assumes tableView is @property 
} 

그리고 이런 식으로 할 수있는 큰 이유에 ...

-(void)refresh { 
    [self doSomeTask]; //calls [taskDone] when finished 
} 

-(void)taskDone { 
    [self.refreshControl endRefreshing]; 
} 

은 그냥 클래스 - 제공 UIRefreshControl에 대한 광범위한 액세스를 제공하므로 endRefreshing 또는 UIRefreshControl의 isRefreshing 속성을 확인할 수 있습니다.

+1

'@property (nonatomic, strong) UIRefreshView * refreshControl;'아마도 UIRefreshControl'을 여기에 사용했을 것입니다. 권리? –

+0

@ self.name 마무리 후 'taskDone'을 호출하는 이유는 무엇입니까? 그것은 나를 놀라게한다. 사실, 그것은'taskDone'을 실행하고 새로 고침을 위해 다시 와야합니다. 맞습니까? +1 당신의 대답. – Ron