1、导入EGO框架,即附件中的EGO.ZIP中的两个类文件,以及一些相应的图片,在第二个tableview.zip文件中
2、我的.h文件
#import <UIKit/UIKit.h>
#import "EGORefreshTableHeaderView.h"
@interface QuanWenArt : UIViewController<UITableViewDelegate, UITableViewDataSource,
EGORefreshTableHeaderDelegate,UIScrollViewDelegate>
{
// CGFloat height;
EGORefreshTableHeaderView *_refreshHeaderView;
BOOL _reloading;
NSInteger cellHeight;
}
@property (nonatomic, retain) UITableView *table;
@property (nonatomic ,retain) NSArray *data;
@end
3.我的.m文件
#import "QuanWenArt.h"
#import "AppDelegate.h"
#import "MyNav.h"
#import "JSON.h"
@interface QuanWenArt ()
@end
@implementation QuanWenArt
@synthesize table = _table;
@synthesize data = _data;
-(void)dealloc
{
[super dealloc];
[_table release];
[_data release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"----------------------------------------------------------");
self.navigationItem.title = @"文章";
// [self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:[UIImage imageNamed:@"navbar.png"]
// forState:UIControlStateNormal
// barMetrics:UIBarMetricsDefault];
// //自定义返回按钮
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 68, 31);
backButton.backgroundColor = [UIColor clearColor];
[backButton setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButton;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"这里是QuanWenArt类的viewDidLoad方法");
self.table = [[[UITableView alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 480.0) style: UITableViewStylePlain] autorelease];
self.table.delegate = self;
self.table.dataSource = self;
self.view = self.table;
//初始化数据源
AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
self.data = delegate.artsOfQWData;
// NSLog(@"list is:%@",self.data);
//增加下拉刷新加载数据 1;
_refreshHeaderView=[[EGORefreshTableHeaderView alloc] initWithFrame:
CGRectMake(0, 440, 320, 480)];
_refreshHeaderView.delegate=self;
[self.table addSubview:_refreshHeaderView];
[_refreshHeaderView refreshLastUpdatedDate];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.data.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"]; //获取cell
[ cell setBackgroundColor:[ UIColor blueColor ] ] ;
//每次都要清空一下,否则cell中的内容会重叠
for (UIView *subview in [cell.contentView subviews])
[subview removeFromSuperview];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"cell"] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //表格单元右侧的披露按钮
cell.selectionStyle = UITableViewCellSelectionStyleGray;
int selectedIndex = [indexPath indexAtPosition: 1]; //获取行号
for (int i=0; i<self.data.count; i++) {
if (i == selectedIndex) {
NSDictionary *dict = [self.data objectAtIndex:i];
// NSString *idstr = [dict objectForKey:@"id"];
NSString *titlestr = [dict objectForKey:@"title"];
// NSString *authorstr = [dict objectForKey:@"author"];
// NSLog(@"id=%@,title=%@,author=%@",idstr,titlestr,authorstr);
cell.textLabel.text = titlestr;
cellHeight = cell.frame.size.height;
break;
}
}
return cell;
}
- (void)backAction
{
[self.navigationController popViewControllerAnimated:YES];
}
//此方法是开始读取数据 增加下拉刷新加载数据 2;
- (void)reloadTableViewDataSource
{
NSLog(@"start");
_reloading = YES;
//打开线程,读取下一页数据
[NSThread detachNewThreadSelector:@selector(requestNext) toTarget:self withObject:nil];
}
//增加下拉刷新加载数据 3;
- (void)requestNext
{
NSAutoreleasePool * pool=[[NSAutoreleasePool alloc] init];
//请求下一页数据
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init];
NSString *strURL = [NSString stringWithFormat:@"http://192.168.1.49:8080/exz/xmlli?fl=exz-qw-tb&op=new&ps=10&pn=2"];
NSURL *connection = [[NSURL alloc] initWithString:strURL];
[req setURL:connection];
[req setHTTPMethod:@"GET"];
NSURLResponse *rep = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req
returningResponse:&rep
error:&error];
if(error)
{
NSLog(@"服务器请求失败");
}else {
if (data) {
NSLog(@"服务器请求成功");
NSString *jsonStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSArray *arr = [jsonStr JSONValue];
AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
[delegate.artsOfQWData addObjectsFromArray:arr];
self.data = delegate.artsOfQWData;
[jsonStr release];
}
}
[pool release];
//回到主线程跟新界面
[self performSelectorOnMainThread:@selector(dosomething) withObject:nil waitUntilDone:YES];
}
//增加下拉刷新加载数据 4;
-(void)dosomething
{
int count=[self.data count];
NSString *str1 = [NSString stringWithFormat:@"%d",count];
NSString *str2 = [NSString stringWithFormat:@"%d",self.table.contentSize.height];
NSLog(@"-->%@",str1);
NSLog(@"-->%@",str2);
//
// if(100*count>2000)
// {
//
// self.table.contentSize=CGSizeMake(320, 40*count);
//设置显示上来加载那一块view的CGRECT
_refreshHeaderView.frame=CGRectMake(0, cellHeight*count, 320, 480);
// }
[self.table reloadData];
[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:0];
}
//此方法是结束读取数据
- (void)doneLoadingTableViewData{
// model should call this when its done loading
_reloading = NO;
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.table];
NSLog(@"end");
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidScroll:self.table];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:self.table];
}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
[self reloadTableViewDataSource];
//[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:0.5];
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
return _reloading; // should return if data source model is reloading
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
return [NSDate date]; // should return date data source was last changed
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end