//從今天開始我給大家介紹下常用的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 [email protected]"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;
}