드롭다운 새로 고침 및 위로 당기기 추가

이전에 한 편의 글을 썼다. 드롭다운 새로 고침의 실현 밑에 새로 고침하는 기능만 이야기했고 이 글은 밑에 새로 고침과 위에 더 많은 기능을 탑재하는 기능만 묘사했다.
 
여기는 핵심 기능의 코드만 열거하고, 다른 코드는 첨부파일을 참고하십시오.
 
DemoTableViewController.h
 
#import <UIKit/UIKit.h>
#import "STableViewController.h"

@interface DemoTableViewController : STableViewController {
    NSMutableArray *items;
}

@end
  
DemoTableViewController.m
 
#import "DemoTableViewController.h"
#import "DemoTableHeaderView.h"
#import "DemoTableFooterView.h"

@interface DemoTableViewController ()
- (void) addItemsOnTop;
- (void) addItemsOnBottom;
- (NSString *) createRandomValue;
@end

@implementation DemoTableViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"STableViewController Demo";
    [self.tableView setBackgroundColor:[UIColor lightGrayColor]];
    
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DemoTableHeaderView" owner:self options:nil];
    DemoTableHeaderView *headerView = (DemoTableHeaderView *)[nib objectAtIndex:0];
    self.headerView = headerView;
    
    nib = [[NSBundle mainBundle] loadNibNamed:@"DemoTableFooterView" owner:self options:nil];
    DemoTableFooterView *footerView = (DemoTableFooterView *)[nib objectAtIndex:0];
    self.footerView = footerView;
    
    items = [[NSMutableArray alloc] init];
    for (int i = 0; i < 10; i++)
        [items addObject:[self createRandomValue]];
}

#pragma mark - Pull to Refresh

- (void) pinHeaderView {
    [super pinHeaderView];
    
    DemoTableHeaderView *hv = (DemoTableHeaderView *)self.headerView;
    [hv.activityIndicator startAnimating];
    hv.title.text = @"Loading...";
}

- (void) unpinHeaderView {
    [super unpinHeaderView];
    
    [[(DemoTableHeaderView *)self.headerView activityIndicator] stopAnimating];
}

- (void) headerViewDidScroll:(BOOL)willRefreshOnRelease scrollView:(UIScrollView *)scrollView {
    DemoTableHeaderView *hv = (DemoTableHeaderView *)self.headerView;
    if (willRefreshOnRelease)
        hv.title.text = @"Release to refresh...";
    else
        hv.title.text = @"Pull down to refresh...";
}

- (BOOL) refresh {
    if (![super refresh])
        return NO;
    
    [self performSelector:@selector(addItemsOnTop) withObject:nil afterDelay:2.0];
    
    return YES;
}

#pragma mark - Load More

- (void) willBeginLoadingMore {
    DemoTableFooterView *fv = (DemoTableFooterView *)self.footerView;
    [fv.activityIndicator startAnimating];
}

- (void) loadMoreCompleted {
    [super loadMoreCompleted];
    
    DemoTableFooterView *fv = (DemoTableFooterView *)self.footerView;
    [fv.activityIndicator stopAnimating];
    
    if (!self.canLoadMore) {
        fv.infoLabel.hidden = NO;
    }
}

- (BOOL) loadMore {
    if (![super loadMore])
        return NO;
    
    [self performSelector:@selector(addItemsOnBottom) withObject:nil afterDelay:2.0];
    
    return YES;
}

#pragma mark - Dummy data methods 

- (void) addItemsOnTop {
    for (int i = 0; i < 3; i++)
        [items insertObject:[self createRandomValue] atIndex:0];
    [self.tableView reloadData];
    
    [self refreshCompleted];
}

- (void) addItemsOnBottom {
    for (int i = 0; i < 5; i++)
        [items addObject:[self createRandomValue]];  
    
    [self.tableView reloadData];
    
    if (items.count > 50)
        self.canLoadMore = NO;
    else
        self.canLoadMore = YES;
    
    [self loadMoreCompleted];
}

- (NSString *) createRandomValue {
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    
    return [NSString stringWithFormat:@"%@ %@", [dateFormatter stringFromDate:[NSDate date]],
            [NSNumber numberWithInt:rand()]];
}

#pragma mark - Standard TableView delegates

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return items.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] autorelease];
    }
    
    cell.textLabel.text = [items objectAtIndex:indexPath.row];  
    return cell;
}

@end

좋은 웹페이지 즐겨찾기