UI控件之UITableView

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上放东西 在公司写的又不能放= = 大家姑且看看吧

github地址: https://github.com/FuThD

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值