2012-10-01 9 views
13

मैं अपने शीर्षलेख के शीर्ष पर UIRefreshControl को स्थानांतरित करने की कोशिश कर रहा हूं या कम से कम इसे सामग्री के साथ काम करने के लिए प्राप्त कर रहा हूं। किसी को भी इसका उपयोग कैसे करना है?UITableView और UIRefreshControl

मैंने तालिका दृश्य में स्क्रॉल करते समय एक अच्छी पृष्ठभूमि रखने के लिए हेडर व्यू का उपयोग किया। मैं स्क्रोल करने योग्य पृष्ठभूमि चाहता था।

- (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 सामान के साथ है नहीं कर रहा हूँ, लेकिन एक UITableView करने के लिए एक UIRefreshControl जोड़ने के मामले में, यह किया जा सकता है। UIRefreshControl वास्तव में UITableViewController के उपयोग के लिए है, लेकिन यदि आप इसे UITableView में सबव्यूव के रूप में जोड़ते हैं तो यह जादुई रूप से काम करता है। कृपया ध्यान दें कि यह अनियंत्रित व्यवहार है, और अन्य आईओएस रिलीज में समर्थित नहीं हो सकता है, लेकिन यह कानूनी है क्योंकि यह कोई निजी एपीआई का उपयोग नहीं करता है।

- (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

इस विधि में कुछ समस्याएं हैं यदि आपके टेबलव्यू में शीर्षक दृश्य (कम से कम आईओएस 7 पर) है। जब आप रीफ्रेश करते समय स्क्रॉल करते हैं, तो तालिका अनुभाग शीर्षक दृश्य की ऊंचाई से ऑफ़सेट होने पर सभी जगहों से बाहर हो जाते हैं। – 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 तक व्यापक पहुंच ताकि आप UIRefreshControl की रीफ्रेशिंग संपत्ति को रीफ्रेशिंग या चेक कर सकें।

+1

'@property (nonatomic, strong) UIRefreshView * refreshControl; 'शायद आप' यूरिफ्रेश कंट्रोल 'का मतलब है। सही? –

+0

@ self.name इसे खत्म करने के बाद 'taskDone' क्यों कॉल करेगा? यह मुझे आश्चर्यचकित करता है। वास्तव में, इसे 'taskDone' निष्पादित करना चाहिए और फिर से ताज़ा करने के लिए वापस आना चाहिए, क्या मैं सही हूँ? हालांकि आपके उत्तर के लिए +1। – Ron