//从今天开始我给大家介绍下常用的tableView的一些功能实现方法,今天就先简单介绍一下tableView的创建
<span style="font-size:18px;">
#import "ViewController.h"
//tableView 要用到的两个代理UITableViewDataSource,UITableViewDelegate
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)style:UITableViewStylePlain];
//这里有两种模式
/**
UITableViewStylePlain, // regular table view
UITableViewStyleGrouped // preferences style table view
*/
//最重要的就是代理了
_tableView.delegate=self;
_tableView.dataSource=self;
[self.viewaddSubview:_tableView];
}
#pragma mark tableView的代理
//tableView的分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
//每组返回多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return10;
}
//设置cell的高度,默认高度64
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return100;
}</span>
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(!cell)
{
//常用加载方法
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
}
cell.textLabel.text =[self.items objectAtIndex:indexPath.row];
return cell;
}
<span style="font-size:18px;">-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//定义一个静态字符串作为cell的复用标识
staticNSString *cellIdentifier =@"cell";
//从复用池中取cell
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
//从Xib加载方法
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MomentsCell" owner:nil options:nil];
cell = [nibs lastObject];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}</span>
//如果忘了在Xib中设置复用标识可以这样设置
NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];
for (id obj in nibObjects)
{
if ([obj isKindOfClass:[CustomTableCell class]])
{
cell = obj;
[cell setValue:cellId forKey:@"reuseIdentifier"];
break;
}
}
//现在又有新方法创建cell了 提前注册一下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell)
{
//将此行代码放在Viewdidload里面也是可以的
[tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil] forCellReuseIdentifier:@"myCell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(MyCustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.leftLabel.text = [self.items objectAtIndex:indexPath.row];
cell.rightLabel.text = [self.items objectAtIndex:indexPath.row];
cell.middleLabel.text = [self.items objectAtIndex:indexPath.row];
}
@end
//这里有两种模式,上面为普通模式下面为分组模式
_tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)style:UITableViewStyleGrouped];
//修改下面代码
//tableView的分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return3;
}
//每组返回多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return3;
}