`
修博龙泉
  • 浏览: 312769 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

uitableview 上提下拉刷新

 
阅读更多
#import <UIKit/UIKit.h>
#import "PullToRefreshTableView.h"

@interface ChyoViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
    PullToRefreshTableView * tableView;  
    NSMutableArray         * array;      //  数据源
}

@property (nonatomic, retain) PullToRefreshTableView * tableView;
@property (nonatomic, retain) NSMutableArray                  * array;

- (void)updateThread:(NSString *)returnKey;
- (void)updateTableView;

@end



//
//  ChyoViewController.m
//  PullToRefresh
//
//  Created by hsit on 12-1-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ChyoViewController.h"

@implementation ChyoViewController

@synthesize tableView;
@synthesize array;

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    array = [[NSMutableArray alloc] init];
    tableView = [[PullToRefreshTableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
   [table setContentSize:CGSizeMake(320, 960)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

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

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)updateThread:(NSString *)returnKey{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    sleep(2);
    switch ([returnKey intValue]) {
        case k_RETURN_REFRESH:
            [array removeAllObjects];
            [array addObject:[NSString stringWithFormat:@"%d", [array count] + 1]];
            break;
            
        case k_RETURN_LOADMORE:
            [array addObject:[NSString stringWithFormat:@"%d", [array count] + 1]];
            break;
            
        default:
            break;
    }
    [self performSelectorOnMainThread:@selector(updateTableView) withObject:nil waitUntilDone:NO];
    [pool release];
}

- (void)updateTableView{
    if ([array count] < 10) {
        //  一定要调用本方法,否则下拉/上拖视图的状态不会还原,会一直转菊花
        [tableView reloadData:NO];
    } else {
        //  一定要调用本方法,否则下拉/上拖视图的状态不会还原,会一直转菊花
        [tableView reloadData:YES];
    }
}

#pragma mark -
#pragma mark Scroll View Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [tableView tableViewDidDragging];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSInteger returnKey = [tableView tableViewDidEndDragging];
    
    //  returnKey用来判断执行的拖动是下拉还是上拖,如果数据正在加载,则返回DO_NOTHING
    if (returnKey != k_RETURN_DO_NOTHING) {
        NSString * key = [NSString stringWithFormat:@"%d", returnKey];
        [NSThread detachNewThreadSelector:@selector(updateThread:) toTarget:self withObject:key];
    }
}

#pragma mark -
#pragma mark Table View DataSource 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
    if ([array count] == 0) {
        //  本方法是为了在数据未空时,让“下拉刷新”视图可直接显示,比较直观
        tableView.contentInset = UIEdgeInsetsMake(k_STATE_VIEW_HEIGHT, 0, 0, 0);
    }
    return [array count];
}

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics