一:初始化UITableView,通过代码或者stroyboard,注意的地方是设置代理和数据源
self.tableView.delegate = self;
self.tableView.dataSource= self;
二:如果要隐藏多余的cell
slef.tableView.tableFooterView = [[UIView alloc]init];
三:实现数据源的两个Required的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//如果是从nib中加载
//cell=[[[NSBundle mainBundle]loadNibNamed:@"PFPersonInfoCell" owner:self options:nil] objectAtIndex:0];
}
return cell;
}
四:另外还可以使用以下配套方法:
1.首先在初始化时注册cell
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
或者用nib相应的注册方法- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
2.然后在数据源中可以这么干就OK了(现在继承UITableView的默认就是下面方法了,注意在sb中cel的Indentifier就ok)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:
indexPath];
return cell;
}
五:剩下的就是可以按需求来玩了,方法可以参照文档