UITableView创建

创建tableview需要签协议UITableViewDataSource, UITableViewDelegate

定义属性 @property (nonatomic, strong)UITableView *tableview;


//创建tableview
self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    //签代理
    _tableview.delegate = self;
    _tableview.dataSource = self;
    //设置行高
    _tableview.rowHeight = 100;
    //分割线
    _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //分割线颜色
    _tableview.separatorColor = [UIColor redColor];
    //添加到父视图
    [self.view addSubview:_tableview];
    //注册cell
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

//头部名称
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"头部名称";
}
//脚部名称
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"脚部名称";
}

也可自定义头部脚部区域
//自定义头部区域
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
    header.backgroundColor = [UIColor redColor];
    return header;
}

//自定义脚部区域
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
    footer.backgroundColor = [UIColor yellowColor];
    return footer;
}

#pragma mark - tableview 必须实现的两个方法
//cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

//设置cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //主标题
    cell.textLabel.text = @"标题";
    //副标题
    cell.detailTextLabel.text = @"副标题";
    //左侧图片
    cell.imageView.image = [UIImage imageNamed:@"qiaoba"];
    //右侧箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //自定义右侧图片
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    img.image = [UIImage imageNamed:@"qiaoba"];
    cell.accessoryView = img;
    
    return cell;
    
}

//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
    
}

//头部高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

//尾部高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}

//tableview右侧索引栏
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return @[@"A",@"B",@"C"];
}

//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消点击效果
    [_tableview deselectRowAtIndexPath:indexPath animated:YES];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
源码JHCellConfig,一种十分灵活易变的适用于创建有多种cell的UITableView的方法,不需要使用switch...case,在调整不同种cell的顺序、及增删某种cell时极其方便 -----本类相当于将tableView中cell所需的基本信息存储下来,以便放到数组中管理 2015.3.30已更新 使用Nib写cell的童鞋,请到github上下载最新代码,新加了一个支持Nib的方法哟! 关于如何使用请看demo,注释写了很多 优点:改变不同类型cell的顺序、增删时,极为方便,只需改变VC中数据源数组即可,无需在多个tableView代理方法中逐个修改 缺点:大家帮我找一找,什么样的需求会难以实现,感谢^_^ 我会不定期更新此demo,加上一些“高级”用法,即应对不同需求时的用法,可以关注下面的微信公众号,更新时我会给你们发消息 · 加入了“高级评论”的示例代码,根据数据源数组来显示评论列表,个数不确定、评论内容长度不确定(即cell高度不确定),可在工程中搜索 高级评论 查看相关代码。 · 加入了cell上按钮触发事件绑定的示例代码,手势等同理。 · 对不同种cell进行不同设置时,通过 其对应的 cellConfig.title 进行判断。 (这样,不论你将dataArray如何修改,插入、删除、改变顺序,都无须再次修改这里的判断) 此方法的核心思想就是两个字 “化简”。 通常写多种cell时,不同cell有不同设置,需要在多个代理方法中进行判断,而且很多是用switch...case来写。这样一旦修改,就需要修改多处。 而通过JHCellConfig类提供的方法进行化简后,要改两种cell的顺序,就只需交换两行对应的代码;要删除或添加某种cell,就只需注释/删除或添加一行代码。 y = x + 3x + 5x 化简-> y = 9x PM 提需求: 把x换成a! y = x + 3x + 5x 需修改3处,y = 9x只需修改1处

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值