UITableView的简单总结及使用心得 - FuThD/FuThD.github.io GitHub Wiki

###UITableView

####一UITableView属性:

1>,@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource;//设置tableView的数据源对象

2>,@property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;

3>,@property (nonatomic) CGFloat rowHeight;//设置cell的行高

4>,@property (nonatomic, strong, nullable) UIColor *separatorColor;//分割线颜色

//例如:
self.tableView.separatorColor = [UIColor redColor];

5>,@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;//分割线样式

//例如:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

6>,@property (nonatomic, strong, nullable) UIView *tableHeaderView;//tableView的头视图

7>,@property (nonatomic, strong, nullable) UIView *tableFooterView;//tableView的尾视图                           

####二UITableView 数据源方法 UITableViewDataSource

pragma mark - /********************* 数据源方法 *********************/
// 1.返回一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

// 2.返回每组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return 4;
}

// 3.返回每行要显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    // 创建一个单元格对象
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    return cell;
}

// 设置组的头部和尾部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   
    return @"啦啦啦";
    
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
   
    return @"哈哈哈";
}

设置tableView右边的标题索引
(nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
 return [self.groups valueForKeyPath:@"title"];
}

####三UITableView 代理方法 UITableViewDelegate

pragma mark - /************************* UITableView代理方法 *************************/
//设置cell的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 0) {
        return 60;
    } else {
        return 100;
    }
}
//当选中某一行cell的时候,调用此方法
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

####四UITableView方法

//UITableView 刷新
//刷新全部数据
- (void)reloadData;

//刷新对应的行
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

####五UITableView的缓存机制 UITableView的缓存机制

//1,定义一个标示
static NSString *ID = @"hero";
// 2,根据标示,去tableView中的缓存池中找单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3,如果没有找到,就创建一个新的cell 
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
NSLog(@"------ 缓存池中找不到cell----%ld", indexPath.row);
    }

####六单元格UITableViewCell

// 创建一个单元格对象
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
 
//1,cell的常见属性:
1>,@property (nonatomic) UITableViewCellAccessoryType    accessoryType;设置cell右边的小箭头

//例如:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

2>,@property (nonatomic, strong, nullable) UIView     *accessoryView;自定义cell右边的view

//例如:
    cell.accessoryView = [[UISwitch alloc] init];

3>,@property(nullable, nonatomic,copy)  UIColor   *backgroundColor;设置cell背景颜色

//例如:
cell.backgroundColor = [UIColor redColor];


4>,@property (nonatomic, strong, nullable) UIView *backgroundView;设置cell的背景视图

//例如:
UIImageView *imgVw = [[UIImageView alloc] init];
imgVw.image = [UIImage imageNamed:@"bg"];
cell.backgroundView = imgVw;


5>,@property (nonatomic, strong, nullable) UIView *selectedBackgroundView;设置cell选中的背景视图

//例如:
UIView *bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor colorWithRed:123 / 255.0 green:196 / 255.0 blue:252 / 255.0 alpha:1.0];
cell.selectedBackgroundView = bgView;
⚠️ **GitHub.com Fallback** ⚠️