#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