UITableView派生自UIScrollView
UITableView结构如下:
背景是滚动视图,每个横向的表格称为cell ( UITableViewCell )
每一个 cell 既可以存储数据,也可以接受选中的事件,
我们选中某个cell时,可以下拉列表,可以推出新的页面
在编辑模式选中多个cell,可以批量删除等。
成员变量
1 { 2 UITableView * _tableV; 3 NSMutableArray * _dataArr; 4 UISearchController * _search; 5 NSMutableArray * _selectCell; 6 }
UITableView创建 (写在方法中,可用 self 调用)
-(void)createTableView
{
_tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStyleGrouped];
//设置UITableView风格 两种风格
/*
UITableViewStylePlain,
UITableViewStyleGrouped
*/
//设置代理
_tableV.delegate = self;
_tableV.dataSource = self;
_tableV.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.view addSubview:_tableV];
}
UITableViewDataSource 协议方法
设置 UITableView cell (required)
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//重定位符
static NSString * str = @"cell";
//取出队列中的cell
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
//如果cell为null ,则创建新的cell
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.textLabel.text = _dataArr[indexPath.row];
return cell;
}
设置 UITableView 分区(section)的cell的数目 (required)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return [[_dataArr objectAtIndex:section] count];
return _dataArr.count;
}
设置 UITableView 分区 (section)的数目 (optional)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
{
return 1;
}
设置 UITableView title header (optional)
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"title";
}
设置 UITableView title footer (optional)
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"footer title";
}
设置 UITableView title footer (optional)
IOS UI学习 UITableView ----- UITableViewDataSource
原文:http://www.cnblogs.com/cccccy/p/4804302.html