UITableView

2016-08-31  本文已影响0人  vip4iPhonr

tableView展示数据

self.tableView.dataSource = self
@interface ViewController () <UITableViewDatatSource>
@end
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
 - (NSInteger)tableView:(UITableViw *)tableView numberOfRowsInSection:(NSInteger)section;
 - (UITableViewCell *)tablView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

tableView常见设置

//tableView有两种样式:
tableView.style = UITableViewStyleGrouped;//分组样式
tableView.style = UITableViewStylePlain;//单组数据
self.tableView.rowHeight = 80;
self.tableView.sectionHeaderHeight = 50;
self.tableView.sectionFooterHeight = 50;
self.tableView.separatorColor = [UIColor redColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.tableHeaderView = [[UISwitch alloc] init];
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
self.tableView.sectionIndexColor = [UIColor redColor];
self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];

tableViewCell常见设置

cell.accessoryView = [[UISwitch alloc] init];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
backgroundView优先级 > backgroundColor
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor blueColor];
cell.backgroundView = bg;
cell.backgroundColor = [UIColor redColor];
UIView *selectbg = [[UIView alloc] init];
selectbg.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectbg;
cell.selectionStyle = UITableViewCellSelectionStyleNone;

tableView代理方法

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:
(NSIndexPath *)indexPat
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
(NSInteger)section
  - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:
(NSInteger)section
  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:
(NSInteger)section
  - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

性能优化

//每当有一个cell进入视野范围内就会调用一次
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.定义一个重用标识
    static NSString *ID = @"wine";
    //2.首先去缓存池查找可循环利⽤的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //3.如里缓存池中没有,自己创建
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    //4.设置数据
    cell.textLabel.text = [NSString stringWithFormat:@"%ld行数据",indexPath.row];
    return cell;
    }
}
NSString *ID = @"id";
 - (void)viewDidLoad {
    [super viewDidLoad];
    // 根据ID 这个标识 注册对应的 cell类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
//每当有一个cell进入视野范围内就会调用一次
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.首先去缓存池查找可循环利⽤的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //2.设置数据
    cell.textLabel.text = [NSString stringWithFormat:@"%zd行数据",indexPath.row];
    return cell;
}

索引条

//返回每一组的索引标题(数组中都是NSString对象)
 - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView
*)tableView

上一篇 下一篇

猜你喜欢

热点阅读