<UITableViewDataSource,UITableViewDelegate>
代理
全局变量
{
UITableView * _tableView;
NSMutableArray * dataArray;
UIRefreshControl * _refresh; //刷新
}
//加载数据
-(void)addData
{
for (int i=0; i<10; i++)
{
[dataArray addObject:[NSString stringWithFormat:@"%d",arc4random()%100]];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化数据源
dataArray=[[NSMutableArray alloc]init];
[self addData];
//创建表格式图
_tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource=self;
_tableView.delegate=self;
[self.view addSubview:_tableView];
//添加刷新控件
_refresh=[[UIRefreshControl alloc]init];
_refresh.attributedTitle=[[NSAttributedString alloc]initWithString:@"正在刷新....."];
//刷新控件 添加事件
[_refresh addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
[_tableView addSubview:_refresh];
}
-(void)refresh
{
//模拟网络延时这个过程 过多久时间去执行某个方法 延迟方法
[self performSelector:@selector(refreshing) withObject:nil afterDelay:2];
}
-(void)refreshing
{
[self addData];
//回到主线程 执行结束的刷新方法
[self performSelectorOnMainThread:@selector(finishedRefresh) withObject:nil waitUntilDone:YES];
}
-(void)finishedRefresh
{
//结束刷新
[_refresh endRefreshing];
//刷新数据
[_tableView reloadData];
NSDate * data=[NSDate date];
NSDateFormatter * matter=[[NSDateFormatter alloc]init];
[matter setDateFormat:@"yyyy:MM:dd hh:mm:ss"];
//将年月日时分秒
NSString * currentDate=[matter stringFromDate:data];
//设置刷新之后的提示语
_refresh.attributedTitle=[[NSAttributedString alloc]initWithString:currentDate];
}
#pragma mark -----协议方法-----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellId=@"cellId";
UITableViewCell * cell=[_tableView dequeueReusableCellWithIdentifier:cellId];
if (cell ==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text=dataArray[indexPath.row];
return cell;
}