Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implementing a cyclic UITableView

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? Examples here might be to pick the day of the week, hour in a 24 hour day, or the timezones ordered sequentially around the globe. Have a few ideas how to hack this (may be say a list of 100x7 days starting from the middle) but nothing elegant.

Does anyone have any ideas or experience of this?

David

like image 449
drw Avatar asked Jan 03 '11 12:01

drw


1 Answers

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 <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
like image 168
nacho4d Avatar answered Sep 29 '22 00:09

nacho4d