What is the best way to implement a cyclic UITableView where instead of showing white space when the user scrolls up to the table\'s bounds, it simply wraps round cyclically
I have seen this behavior a couple of times but not in a UITableView it was a UIPickerView. The code is quite simple and probably convertible to a UITableView....
The code for ciclic UIPickerView
RollerViewController.h
@interface RollerViewController : UIViewController {
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