- 2 2 4
// UITableView有兩種風格,UITableViewStylePlain,UITableViewStyleGrouped
// UITableView有兩個代理,<UITableViewDataSource,UITableViewDelegate>,table.dataSource = self;table.delegate = self;//兩個代理
// UITableView有四個cell風格,UITableViewCellStyleDefault,UITableViewCellStyleSubtitle,UITableViewCellStyleValue1,UITableViewCellStyleValue2
- UITableViewDataSource的方法重點,@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
return self.arr.count;
}
//每一行都要執行一遍,UITableViewDataSource的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
static NSString *identity = @"cell";
UITableViewCell *cell;
if(cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identity];
}
//UITableViewCellStyleDefault 這種形式是一張圖,一行文字,也可自行減少
//如果有詳情的話,換成UITableViewCellStyleSubtitle
Person *p = self.arr[indexPath.row];
cell.textLabel.text = p.data;
cell.detailTextLabel.text = p.detail;
cell.imageView.image = [UIImage imageNamed:p.imagename];
NSLog(@"row==%ld,cell==%p",indexPath.row,cell );
cell.accessoryType = UITableViewCellAccessoryCheckmark;//屬性為右邊對號,感嘆號等樣式
return cell;
}
3.上拉,下拉,刷新(第三方)方法
//開啟刷新狀態
[self setupRefresh];
//開始刷新自定義方法
- (void)setupRefresh
{
//下拉刷新
[self.table addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
[self.table headerBeginRefreshing];
// 上拉加載更多(進入刷新狀態就會調用self的footerRereshing)
[self.table addFooterWithTarget:self action:@selector(footerRereshing)];
//一些設置
// 設置文字(也可以不設置,默認的文字在MJRefreshConst中修改)
self.table.headerPullToRefreshText = @"下拉可以刷新了";
self.table.headerReleaseToRefreshText = @"松開馬上刷新了";
self.table.headerRefreshingText = @"刷新中。。。";
self.table.footerPullToRefreshText = @"上拉可以加載更多數據了";
self.table.footerReleaseToRefreshText = @"松開馬上加載更多數據了";
self.table.footerRefreshingText = @"加載中。。。";
}
//下拉刷新
- (void)headerRereshing
{
//一般這些個里邊是網絡請求,然后會有延遲,不會像現在刷新這么快
[self performSelector:@selector(request) withObject:nil afterDelay:5];
}
- (void)request{
// 添加假數據
//[self.arr insertObject:@"這是刷新的數據" atIndex:0];
[self.table reloadData];
//結束刷新
[self.table headerEndRefreshing];
}
//上拉加載
- (void)footerRereshing
{
//這個一般是提取緩存的數據
// 添加假數據
//[self.arr insertObject:@"這是加載以前的數據" atIndex:[self.arr count]];
[self.table reloadData];
//結束刷新
[self.table footerEndRefreshing];
}