2011-01-03 23 views
6

को लागू करने के लिए चक्रीय UITableView को लागू करने का सबसे अच्छा तरीका क्या है, जहां उपयोगकर्ता टेबल की सीमा तक स्क्रॉल करते समय सफेद स्थान दिखाने की बजाय, यह बस चक्रीय रूप से घूमता है? यहां उदाहरण सप्ताह के दिन, 24 घंटे के दिन में घंटे, या समय-समय पर क्रमशः आदेश दिया गया समय चुनने का विकल्प हो सकता है। कुछ विचार करें कि इसे कैसे हैक करें (मध्य से शुरू होने वाले 100x7 दिनों की सूची कहें) लेकिन कुछ भी सुरुचिपूर्ण नहीं है।एक चक्रीय UITableView

क्या किसी के पास इसका कोई विचार या अनुभव है?

डेविड

उत्तर

-3

आप चक्रीय तालिका नहीं बना सकते हैं। मान लीजिए कि आप संख्या OfRowsInSection विधि (सीमित संख्या) से बड़ी संख्या में पंक्तियां लौटाते हैं, फिर भी ऊपरी छोर वाले टेबल और cource के साथ आप पंक्तियों की संख्या के लिए अनंत वापस नहीं लौट सकते हैं, जिससे यह समाप्त हो रहा है। तो यह गोल मेज की तरह दिख नहीं सकता है।

आप इसे सीमित संख्या के लिए भी कर सकते हैं, आप भी पंक्तियों को दोहरा सकते हैं लेकिन चक्रीय तालिका बनाना संभव नहीं है।

+0

मुझे लगता है कि यह यह है। यहां करने के लिए कुछ भी सुरुचिपूर्ण नहीं है; कम से कम पंक्तियां सभी वर्चुअल हैं ताकि आप उन्हें फ्लाई पर बना सकें। – drw

+0

@drw: कृपया इस लिंक का अनुसरण करें: [लिंक] (http://stackoverflow.com/questions/5675535/how-to-implement-cyclic-scrolling-on-tableview) –

+0

नीचे मेरा उत्तर देखें। यह कोड मिला है। मैं उस कोड का उपयोग लंबे समय से कर रहा हूं। यह काम करता हैं। –

4

मैंने इस व्यवहार को दो बार देखा है लेकिन UITableView में नहीं, यह एक UIPickerView था। कोड काफी सरल है और शायद एक UITableView के लिए परिवर्तनीय है ....

ciclic UIPickerView के लिए कोड

RollerViewController.h

@interface RollerViewController : UIViewController <UIPickerViewDelegate>{ 
    UIPickerView *picker; 
}  
@end 

RollerViewController.m

#import "RollerViewController.h" 

@implementation RollerViewController 

#define MAX_ROLL 100 
#define ROWS_COUNT 10 

#pragma mark - 
#pragma mark Helpers 

- (void) infinitePickerViewDidSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 
    NSUInteger base10 = (MAX_ROLL/2) - (MAX_ROLL/2)%ROWS_COUNT; 
    [picker selectRow:row%ROWS_COUNT+base10 inComponent:component animated:FALSE]; 
} 

#pragma mark - 
#pragma mark UIPickerView dataSource delegate methods 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { 
    return 3; 
} 
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { 
    return MAX_ROLL; 
} 
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ 
    return (CGFloat)40; 
} 
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    [self infinitePickerViewDidSelectRow:row inComponent:component]; 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ 
    return (CGFloat) 50; 
} 
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row 
      forComponent:(NSInteger)component reusingView:(UIView *)rview { 

    UILabel *retval = (UILabel *)rview; 
    if (!retval) { 
     retval= [[[UILabel alloc] initWithFrame:CGRectMake(5,5,40,30) ] autorelease]; 
    } 

    retval.text = [NSString stringWithFormat:@"%d", row%ROWS_COUNT]; 
    retval.font = [UIFont systemFontOfSize:25]; 
    retval.textAlignment = UITextAlignmentCenter; 
    retval.backgroundColor = [UIColor clearColor]; 
    return retval; 
} 

#pragma mark overides 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 280)]; 
    picker.delegate = self; 
    [self.view addSubview:picker]; 

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

    [self infinitePickerViewDidSelectRow:arc4random()%MAX_ROLL inComponent:0]; 
    [self infinitePickerViewDidSelectRow:arc4random()%MAX_ROLL inComponent:1]; 
    [self infinitePickerViewDidSelectRow:arc4random()%MAX_ROLL inComponent:2]; 
} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (void)dealloc { 
    [picker release]; 
    [super dealloc]; 
} 

@end 
2

हाँ, एक चक्रीय UITableView बनाना संभव है। आप बस पंक्तियों की संख्या के रूप में वास्तव में बड़ी संख्या में पास करते हैं और फिर तालिका पंक्ति अनुरोधों के प्रत्येक पंक्ति के लिए, आप इसे row_number% number_of_rows पास करते हैं, इसलिए यदि आप पंक्ति संख्या हास्यास्पद रूप से बड़ी होती है तो भी आप हमेशा अपने डेटा में फिर से चल रहे होते हैं।

आप यहाँ एक उदाहरण देख सकते हैं: http://dev.doukasd.com/2011/04/infinite-scrolling-dial-control-for-ios/

मूल रूप से यह UIPickerView की तरह एक UITableView के साथ लागू किया है।

4

UITableView scrollViewDidScroll विधि में UIScrollView के समान है।

तो, अनंत स्क्रॉलिंग अनुकरण करना आसान है।

  1. डबल सरणी ताकि सिर और पूंछ एक साथ शामिल हो गए हैं गोल मेज का अनुकरण करने

  2. दोगुनी मेज और दोगुनी तालिका के 2 भाग जब वे करते हैं के 1 भाग के बीच उपयोगकर्ता स्विच करने के लिए मेरे निम्नलिखित कोड का उपयोग टेबल की शुरुआत या अंत तक पहुंचने के लिए।

:

/* To emulate infinite scrolling... 

The table data was doubled to join the head and tail: (suppose table had 1,2,3,4) 
1 2 3 4|1 2 3 4 (actual data doubled) 
--------------- 
1 2 3 4 5 6 7 8 (visualising joined table in eight parts) 

When the user scrolls backwards to 1/8th of the joined table, user is actually at the 1/4th of actual data, so we scroll instantly (we take user) to the 5/8th of the joined table where the cells are exactly the same. 

Similarly, when user scrolls to 6/8th of the table, we will scroll back to 2/8th where the cells are same. (I'm using 6/8th when 7/8th sound more logical because 6/8th is good for small tables.) 

Thus, when user reaches 1/4th of the first half of table, we scroll to 1/4th of the second half, when he reaches 2/4th of the second half of table, we scroll to the 2/4 of first half. This is done simply by subtracting OR adding half the length of the new/joined table. 
*/ 


-(void)scrollViewDidScroll:(UIScrollView *)scrollView_ 
{ 

    CGFloat currentOffsetX = scrollView_.contentOffset.x; 
    CGFloat currentOffSetY = scrollView_.contentOffset.y; 
    CGFloat contentHeight = scrollView_.contentSize.height; 

    if (currentOffSetY < (contentHeight/8.0)) { 
    scrollView_.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2))); 
    } 
    if (currentOffSetY > ((contentHeight * 6)/ 8.0)) { 
     scrollView_.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2))); 
    } 

} 

पी.एस. - मैंने एनटी टाइम टेबल (लाइट) नामक मेरे ऐप्स में से एक पर इस कोड का उपयोग किया है। यदि आप पूर्वावलोकन चाहते हैं, तो आप ऐप देख सकते हैं: https://itunes.apple.com/au/app/nt-time-table-lite/id528213278?mt=8

यदि आपकी तालिका कभी-कभी बहुत कम हो सकती है, तो उपर्युक्त विधि की शुरुआत में आप डेटा गिनती के लिए विधि से बाहर निकलने के लिए तर्क जोड़ सकते हैं 9 से कम उदाहरण।

0

मैंने UIScrollView के आधार पर एक चक्रीय तालिका बनाई है। और इस तालिका के आधार पर, मैं UIPickerView को फिर से कार्यान्वित करता हूं। आपको इस कक्षा DLTableView में रुचि हो सकती है। https://github.com/danleechina/DLPickerView