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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值