1,在一个普通的上图控制器上面添加一个tableview
list
@interface ListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *tableview;
NSMutableArray *ds;
UISwipeGestureRecognizer *leftRecognizer;
UISwipeGestureRecognizer *rightRecognizer;
}
@end
2,绑定这个tableview的代理
tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, WINDOW_HEIGHT)];
tableview.dataSource= self;
tableview.delegate=self;
3,设置相关的数据源
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *cellValue =[ds objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor greenColor];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ds.count;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%ld",(long)indexPath.row+1);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
@end
实现原理:
在任意一个控制器里面都可以添加上tableview,并且将tableview的数据源与这个控制器绑定,然后再下面写代理里面的方法:cell、number、之类的数据源绑定方法;这样这个tableview就在这个视图里面加载成功了。