2010-03-23 3 views
1

열차 도착 시간 참조 앱을 만들어 다른 방송국을 선택하고 A에서 B까지의 시간과 B에서 A까지의 시간을 표시 할 수있게했습니다. 따라서 각 스테이션마다 여러 페이지를 갖고 싶기 때문에 Apple의 Page Control을 사용하고 각 페이지는 모든 기차 시간을 나열합니다. 내 appdelegate 여기있어 :사용자 정의 셀이있는 UITableView는 한 번에 한 행의 데이터 만로드합니다.

#import "AppDelegate.h" 
#import "MyViewController.h" 

static NSUInteger kNumberOfPages = 2; 

@interface AppDelegate (PrivateMethods) 

- (void)loadScrollViewWithPage:(int)page; 
- (void)scrollViewDidScroll:(UIScrollView *)sender; 

@end 

@implementation AppDelegate 

@synthesize window, scrollView, pageControl, viewControllers; 

- (void)dealloc { 
     [viewControllers release]; 
     [scrollView release]; 
     [pageControl release]; 
     [window release]; 
     [super dealloc]; 
} 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
     // view controllers are created lazily 
     // in the meantime, load the array with placeholders which will be replaced on demand 
     NSMutableArray *controllers = [[NSMutableArray alloc] init]; 
     for (unsigned i = 0; i < kNumberOfPages; i++) { 
       [controllers addObject:[NSNull null]]; 
     } 
     self.viewControllers = controllers; 
     [controllers release]; 

     // a page is the width of the scroll view 
     scrollView.pagingEnabled = YES; 
     scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height); 
     scrollView.showsHorizontalScrollIndicator = NO; 
     scrollView.showsVerticalScrollIndicator = NO; 
     scrollView.scrollsToTop = NO; 
     scrollView.delegate = self; 

     pageControl.numberOfPages = kNumberOfPages; 
     pageControl.currentPage = 0; 

     // pages are created on demand 
     // load the visible page 
     // load the page on either side to avoid flashes when the user starts scrolling 
     [self loadScrollViewWithPage:0]; 
     [self loadScrollViewWithPage:1]; 
} 

- (void)loadScrollViewWithPage:(int)page { 
     if (page < 0) return; 
     if (page >= kNumberOfPages) return; 

     // replace the placeholder if necessary 
     MyViewController *controller = [viewControllers objectAtIndex:page]; 
     if ((NSNull *)controller == [NSNull null]) { 
       controller = [[MyViewController alloc] initWithPageNumber:page]; 
       [viewControllers replaceObjectAtIndex:page withObject:controller]; 
       [controller release]; 
     } 

     // add the controller's view to the scroll view 
     if (nil == controller.view.superview) { 
       CGRect frame = scrollView.frame; 
       frame.origin.x = frame.size.width * page; 
       frame.origin.y = 0; 
       controller.view.frame = frame; 
       [scrollView addSubview:controller.view]; 
     } 
} 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
     // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in 
     // which a scroll event generated from the user hitting the page control triggers updates from 
     // the delegate method. We use a boolean to disable the delegate logic when the page control is used. 
     if (pageControlUsed) { 
       // do nothing - the scroll was initiated from the page control, not the user dragging 
       return; 
     } 

     // Switch the indicator when more than 50% of the previous/next page is visible 
     CGFloat pageWidth = scrollView.frame.size.width; 
     int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
     pageControl.currentPage = page; 

     // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) 
     [self loadScrollViewWithPage:page - 1]; 
     [self loadScrollViewWithPage:page]; 
     [self loadScrollViewWithPage:page + 1]; 

     // A possible optimization would be to unload the views+controllers which are no longer visible 
} 

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 
     pageControlUsed = NO; 
} 

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
     pageControlUsed = NO; 
} 

- (IBAction)changePage:(id)sender { 
     int page = pageControl.currentPage; 

     // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) 
     [self loadScrollViewWithPage:page - 1]; 
     [self loadScrollViewWithPage:page]; 
     [self loadScrollViewWithPage:page + 1]; 

     // update the scroll view to the appropriate page 
     CGRect frame = scrollView.frame; 
     frame.origin.x = frame.size.width * page; 
     frame.origin.y = 0; 
     [scrollView scrollRectToVisible:frame animated:YES]; 

     // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above. 
     pageControlUsed = YES; 
} 

@end 

응용 프로그램 대리인은 "MyViewController"를로드합니다. 내보기 컨트롤러와 함께 뭘 하려는지, 정보의 다른 세트와 두 개의 별도의 정상적인 테이블이 있습니다. 나는 이것을 성취 할 수 있었다.

그런 다음 두 개의 표를 수정하여 설정할 수있는 두 개의 레이블로 구성된 맞춤 셀을 만들었습니다. 여기 MyViewControler이다

#import "MyViewController.h" 
#import "CustomCell.h" 

static NSArray *__pageControlColorList = nil; 
static NSArray *__stationNameList = nil; 

@implementation MyViewController 

@synthesize stationName; 
@synthesize homeBound; 
@synthesize workBound; 
@synthesize homeBoundTimes; 
@synthesize workBoundTimes; 
@synthesize Custom; 

// Creates the color list the first time this method is invoked. Returns one color object from the list. 
+ (UIColor *)pageControlColorWithIndex:(NSUInteger)index { 
    if (__pageControlColorList == nil) { 
     __pageControlColorList = [[NSArray alloc] initWithObjects:[UIColor colorWithRed:16.0/255 green:102.0/255 blue:73.0/255 alpha:1.0], 
                    [UIColor colorWithRed:248.0/255 green:155.0/255 blue:70.0/255 alpha:1.0], nil]; 
    } 

    // Mod the index by the list length to ensure access remains in bounds. 
    return [__pageControlColorList objectAtIndex:index % [__pageControlColorList count]]; 
} 

//Creates the station list the first time this method is invoked. Returns one string object from the list. 
+ (NSString *)pageControlStationWithIndex:(NSUInteger)index { 
    if (__stationNameList == nil) { 
     __stationNameList = [[NSArray alloc] initWithObjects:[NSString stringWithFormat:@"Bramalea"], [NSString stringWithFormat:@"Erindale"], nil]; 
    } 

    //Mod the index by the list length to ensure access remains in bounds. 
    return [__stationNameList objectAtIndex:index % [__stationNameList count]]; 
} 

//Creates the station list the first time this method is invoked. Returns one string object from the list. 

+ (NSArray *)HomeTimesByIndex:(NSUInteger)index { 
    if (index == 0) {  
     return [[NSArray alloc] initWithObjects:@"5:55 am", @"6:49 am", @"7:14 am", @"7:30 am", @"7:38 am", 
               @"8:07 am", @"9:20 am", @"10:25 am", @"11:06 am", @"12:15 pm", 
               @"1:45 pm", @"2:51 pm", @"3:51 pm", @"4:51 pm", @"5:51 pm", 
               @"6:51 pm", @"7:21 pm", @"7:51 pm", @"8:51 pm", @"9:51 pm", nil]; 
    } else { 

     return [[NSArray alloc] initWithObjects:@"5:10 am", @"5:45 am", @"6:45 am", @"7:10 am", 
               @"7:30 am", @"7:45 am", @"8:00 am", @"8:15 am", 
               @"8:30 am", @"9:05 am", @"9:40 am", @"10:15 am", 
               @"10:45 am", @"11:15 am", @"11:45 am", @"12:15 pm", 
               @"12:45 pm", @"1:15 pm", @"1:45 pm", @"2:15 pm", @"2:45 pm", 
               @"3:15 pm", @"3:45 pm", @"4:15 pm", @"5:20 pm", @"6:20 pm", 
               @"7:15 pm", @"8:15 pm", @"9:15 pm", @"10:15 pm", @"11:15 pm", nil]; 
    } 
} 

+ (NSArray *)WorkTimesByIndex:(NSUInteger)index { 
    if (index == 0) {  
     return [[NSArray alloc] initWithObjects:@"6:25 am", @"7:30 am", @"8:30 am", @"9:40 am", @"11:30 am", @"1:00 pm", @"1:50 pm", 
               @"2:00 pm", @"2:45 pm", @"4:15 pm", @"4:45 pm", @"5:15 pm", @"5:45 pm", @"6:45 pm", 
               @"7:20 pm", @"7:35 pm", @"7:55 pm", @"8:15 pm", @"8:35 pm", @"9:00 pm", @"9:30 pm", 
               @"10:00 pm", @"10:30 pm", @"11:00 pm", @"11:30 pm", @"12:01 am", @"12:30 am", @"1:30 am", nil]; 
    } else { 
     return [[NSArray alloc] initWithObjects: 
     @"6:20 am", @"7:20 am", @"8:20 am", @"9:20 am", @"10:20 am", @"11:20 am", @"12:20 pm", 
       @"12:50 pm", @"1:20 pm", @"1:40 pm", @"2:00 pm", @"2:20 pm", @"2:40 pm", @"2:55 pm", 
     @"3:10 pm", @"3:25 pm", @"4:30 pm", @"4:50 pm", @"5:10 pm", @"5:25 pm", @"5:40 pm", 
     @"6:10 pm", @"7:00 pm", @"7:25 pm", @"7:40 pm", @"7:55 pm", @"8:10 pm", @"8:30 pm", 
     @"8:50 pm", @"9:10 pm", @"9:30 pm", @"9:50 pm", @"10:20 pm", @"11:20 pm", @"12:20 am", @"1:20 am", nil]; 
    } 
} 

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

    CGRect frame = self.view.frame; 
    frame.size.height = 600; 
    self.view.frame = frame; 
} 


// Load the view nib and initialize the pageNumber ivar. 
- (id)initWithPageNumber:(int)page { 
    if (self = [super initWithNibName:@"Station" bundle:nil]) { 
     pageNumber = page; 
    } 
    return self; 
} 

- (void)dealloc { 
    [stationName release]; 
    [homeBound release]; 
    [workBound release]; 

    [homeBoundTimes release]; 
    [workBoundTimes release]; 

    [Custom release]; 

    [super dealloc]; 
} 


// Set the label and background color when the view has finished loading. 
- (void)viewDidLoad { 
    //Get station name and set station texts 
    stationName.text = [MyViewController pageControlStationWithIndex:pageNumber]; 
    homeBound.text = [NSString stringWithFormat:@"Next Train From Union to %@:", [MyViewController pageControlStationWithIndex:pageNumber]]; 
    workBound.text = [NSString stringWithFormat:@"Next Train From %@ to Union:", [MyViewController pageControlStationWithIndex:pageNumber]]; 
    self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber]; 

    NSArray *arrayHome = [MyViewController HomeTimesByIndex:pageNumber]; 
    NSArray *arrayWork = [MyViewController WorkTimesByIndex:pageNumber]; 

    self.homeBoundTimes = arrayHome; 
    self.workBoundTimes = arrayWork; 

    [arrayHome release]; 
    [arrayWork release]; 

} 

#pragma mark - 
#pragma mark Table View Data Source Methods 
- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section { 

    if ([tableView tag] == 0) { 
     return [self.homeBoundTimes count]; 
    } else if ([tableView tag] == 1) { 
     return [self.workBoundTimes count]; 
    } 

    return 0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 

    if (cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     cell = self.Custom; 
    } 

    NSUInteger row = [indexPath row]; 

    if ([tableView tag] == 0) { 
     cell.arriveTimeLabel.text = [homeBoundTimes objectAtIndex:row]; 
     cell.departTimeLabel.text = [homeBoundTimes objectAtIndex:row]; 
    } else if ([tableView tag] == 1) { 
     cell.arriveTimeLabel.text = [workBoundTimes objectAtIndex:row]; 
     cell.departTimeLabel.text = [workBoundTimes objectAtIndex:row];   
    } 


    return cell; 

} 


@end 

테이블에로드되는 값은 이제 (DB를 다음 실험)에 대한 하드 코딩된다.

내 문제는 테이블을 볼 때 첫 번째 행에만 데이터가 있고 스크롤 다운을 시작할 때 나머지 데이터 "sorta"가로드된다는 것입니다. "sorta"는 한 번에 첫 번째 행만로드합니다. 모든 데이터를 볼 수있는 유일한 시간은 아래로 스크롤 한 다음 다시 스크롤하거나 행을 선택할 때입니다. 그러나 그 정보는 항상 머물러 있지 않습니다. 때로는 스크롤 할 때 특정 행을 다시 스크롤 할 때까지 정보가 손실됩니다.

아무도 이런 일이 발생하지 않는 이유가 있습니까?

편집 : 짐의 대답은 트릭을 만들었습니다. 여기 내 수정 방법입니다 : 테이블보기 세포 스크롤로 인해 내용을 변경 곳에 내가 본 모든 경우에

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellIdentifier"]; 
    if (cell == nil) { 
     // Load the top-level objects from the custom cell XIB. 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
     cell = [topLevelObjects objectAtIndex:0]; 

     NSUInteger row = [indexPath row]; 

     if ([tableView tag] == 0) { 
      cell.arriveTimeLabel.text = [homeBoundTimes objectAtIndex:row]; 
      cell.departTimeLabel.text = [homeBoundTimes objectAtIndex:row]; 
     } else if ([tableView tag] == 1) { 
      cell.arriveTimeLabel.text = [workBoundTimes objectAtIndex:row]; 
      cell.departTimeLabel.text = [workBoundTimes objectAtIndex:row];   
     } 

    } 

    return cell; 
} 

답변

0

, 그것은 재사용 식별자에 문제가 발생하고있다.

Interface Builder에서 UITableViewCell을 검사하십시오. "Identifier"필드를보십시오. dequeueReusableCellWithIdentifier : ("CustomCellIdentifier")에 전달할 문자열과 일치하는지 확인하십시오.

앱에 다르게 구성된 셀이있는 다른 테이블이있는 경우 해당 셀에 다른 재사용 식별자를 사용하고 있는지 확인하십시오.

관련 문제