tableView的理解以及tableViewCell的使用

tableView的格式区别

选择UITableViewStyleGrouped方式会全部显示,UITableViewStylePlain则在 上拉的时候,把组头顶在顶部
使用UITableViewStyleGrouped风格时,上面会出现 headView ,大概占了35 个像素。
tableView.tableHeaderView =
[[UIView alloc initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];//CGFLOAT_MIN一个宏,

可以去掉头部的headView

tableViewCell的辅助视图 ##

cell.accessoryType = UITableViewCellAccessoryNone;//cell没有任何的样式

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素;

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button;

cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右边的形状是对号

点击cell时没有点击效果

cell.selectionStyle = UITableViewCellSelectionStyle.None

隐藏分割线

tableView.separatorStyle = UITableViewCellSeparatorStyle.None

隐藏多余的cell

tableView.tableFooterView = UIView(frame: CGRectZero)//隐藏下面多出来的cell

cell的复用

对于一组cell, 想要改变其中一个cell的背景色,用cell的tag标记 ,例如

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
     cell.myLabel.text = [NSString stringWithFormat:@"我的Label%ld", indexPath.row];
      cell.tag = indexPath.row;
      if (cell.tag == 5) {
          cell.imageVIew.backgroundColor = [UIColor greenColor];
     }
      if (cell.tag != 5) {        
      cell.imageVIew.backgroundColor = [UIColor whiteColor    

      }
     return cell;
 }

如果不标记,用 indexPath.row判断 在复用cell时会造成其他cell因为是同一个reuseIdentifier而重用。

下面提供一种自定义cell的方式
注意一种cell用一个reuseIdentifier 重用标识符

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = @"cell";

    if(indexPath.section == 0){
        UITableViewCell *cell01 = [_tableView dequeueReusableCellWithIdentifier:@"cell01"];
        if(cell01 == nil){
            cell01 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell01"];
           // cell01.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

            UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(135, 70, 50, 25)];
            btn2.layer.masksToBounds = YES;
            btn2.layer.borderWidth = 0.9;
            btn2.layer.borderColor = [UIColor grayColor].CGColor;
            btn2.layer.cornerRadius = 10;
            [btn2 setTitle:@"Lv10" forState:UIControlStateNormal];
            [btn2 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(330, 20, 80, 40)];
            btn.layer.masksToBounds = YES;
            btn.layer.borderWidth = 0.5;
            btn.layer.borderColor = [UIColor grayColor].CGColor;
            btn.layer.cornerRadius = 5;
            [btn setTitle:@"签到" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"签到图"] forState:UIControlStateNormal];
            [btn setTitle:@"已签到" forState:UIControlStateSelected];
//            [btn setImage:nil forState:UIControlStateSelected];

            [btn addTarget:self action:@selector(pressRight:) forControlEvents:UIControlEventTouchUpInside];

            [cell01.contentView addSubview:btn2];
            [cell01.contentView addSubview:btn];
        }
        cell01.textLabel.text = @"阡陌";
        cell01.imageView.image = [UIImage imageNamed:@"头像.jpeg"];
        return cell01;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值