iOS开发之UI开发(UITableView)

UITableView基本使用

  • 遵守UITableViewDataSourceUITableViewDelegate协议
/// 返回section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

/// 返回每个UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	// 建议使用该方法创建,减少开销
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    return cell;
}

/// UITableView懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = self.view.frame;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 行高
        _tableView.rowHeight = 100;
        // 注册UITableViewCell
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    }
    return _tableView;
}

UITableView常见属性

  • separatorColor 分割线颜色
	self.tableView.separatorColor = [UIColor redColor];
  • separatorStyle 分割线样式
	self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//没有分割线
  • 设置分割线的Insets上下左右间距
	[self.tableView setseparatorInset:UIEdgeInsetsMake(0,0,0,0)];
  • 取消UITabelView的选中高亮
	tableView.allowsSelection = NO;
  • 滚动到最后一行
	[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]
  • 实现右侧的索引栏,,点击右侧索引栏中的文字,会根据索引的顾序跳转到左侧对应的位置,获取groups数组中的每个对象的title值,并返回到一个NSArray
/// 该方法运行一次
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
	// 返回array数组的每个元素对象的属性
	return [self.groups valueForKeyPath:@"title"];
}

UITableViewCell的常见属性

  • lmageView 显示图片

  • textLabel 显示文字

  • detailTextLabel 显示小文字

  • accessoryType 右边单元格类型选择

  • accessoryView 右边单元格自定义

  • backgroundColor设置单元格背景

  • backgroundView设置背景图片,可以指定一个UIImageView

  • selectedBackgroundView设置背景被选中图片

  • accessoryView可以自定义

自定义UITableViewCell

  • 重写构造函数
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentfier {}

Xib在中UITableViewCell的复用

  • 注册UITableViewCell
[tableView registerNib:[UINib nibWithNibName:@"JJTableCell" bundle:nil] forCellReuseIdentifier:@"JJTableCell"];
  • 创建方法还是一样
JJTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JJTableCell"];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值