IOS UI学习 UITableView ----- UITableViewDataSource
UITableView简介
文字简介
UITableView派生自UIScrollView
UITableView结构如下:
背景是滚动视图,每个横向的表格称为cell ( UITableViewCell )
每一个 cell 既可以存储数据,也可以接受选中的事件,
我们选中某个cell时,可以下拉列表,可以推出新的页面
在编辑模式选中多个cell,可以批量删除等。
设置UITableView表头和表尾
- (void)ceateTitle { UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)]; label1.text = @"这里是表头,不是标头"; label1.textAlignment = NSTextAlignmentCenter; label1.backgroundColor = [UIColor cyanColor]; //设置表头 _tableV.tableHeaderView = label1; UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)]; label2.text = @"这里是表尾,不是彪尾"; label2.textAlignment = NSTextAlignmentCenter; label2.backgroundColor = [UIColor cyanColor]; //设置表尾 _tableV.tableFooterView = label2; }
成员变量
1 { 2 UITableView * _tableV; 3 NSMutableArray * _dataArr; 4 UISearchController * _search; 5 NSMutableArray * _selectCell; 6 }
UITableView创建
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]; }
这个怎么分类 UIViewController生命周期方法 用于响应视图编辑状态变化
-(void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [_tableV setEditing:editing animated:animated]; }
UITableViewDataSource 协议方法
设置 UITableView cell (required)
通常自己新建一个新的类通过继承于UITableViewCell来定制新的cell
-(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"; }
设置 滑动cell时 cell 右端是否出现 delete 按钮
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
设置UITableView的编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { }
返回索引数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { if (_tableView == tableView) { NSMutableArray * arr = [[NSMutableArray alloc]init]; //[arr addObject:UISearchBarIconSearch]; for (int i = 0; i < 26; i++) { NSString * str = [NSString stringWithFormat:@"%c",'A' + i]; [arr addObject:str]; } return arr; } else { return nil; } }
未完待续。