美文网首页
iOS - UITableView

iOS - UITableView

作者: CDLOG | 来源:发表于2018-08-17 14:59 被阅读20次

    基本设置

    self.myTableView.bounces = NO; // 取消弹簧效果
    不要分割线:
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    选中不显示:
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //不显示垂直滚动条
    self.myTableView.showsVerticalScrollIndicator = NO;
    //右侧小箭头
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //默认选中TableView第一行,没有动画及滚动效果,动画即滚动效果通过animated,position来设定
    
     [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    
    //获取当前选中的cell
    
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    获取想要的cell
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] ];
    

    分组

    分组的类型选plain才不会有headView显示的错误,尽量不用headview不同机型容易出问题

    头部设置

    //返回每组头部view
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        NSString *str = [NSString stringWithString:_Mset[section]] ;
        NSArray *arr = [str componentsSeparatedByString:@"-"];
        str = [NSString stringWithFormat:@"  %@月%@日",arr[1],arr[2]];
        UIView *headerView = [[UIView alloc]init];
        headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        UILabel *label = [[UILabel alloc]init];
        label.textColor = [UIColor grayColor];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = color_light_gray;
        label.frame = CGRectMake(10, 6, 100, 20);
        
        label.text = str;
        [headerView addSubview:label];
        return headerView;
    }
    

    分组取一

    self.currentExpress =  [self.dictExpress objectForKey:self.firstPinyinArr[indexPath.section]];
    self.currentModel = self.currentExpress[indexPath.row];
    

    选中指定行并跳转

    [self tableView:self.FirstCategoryTable didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectIndex inSection:0]];
        [self.FirstCategoryTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    

    iOS监听tableView组头切换事件

    
    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 组头将要出现的时候系统会调用;
    
    - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section 组头出现的时候系统会调用;
    
    利用以上两个方法可以判断出组头被顶出和组头又下拉回来事件,还有其他的组头相关动作可以监听需自己去编写。
    
    

    cell背景透明

    1,设置控制器的背景色
    2,设置tableview透明
    3,设置cell透明
    4,设置cell 的view不透明

    拿到Cell的值

    cell的.h方法获取cell的一些控件值

    带圆角的多个cell(只有前后有圆角)

    self.currentExpress =  [self.dictExpress objectForKey:self.firstPinyinArr[indexPath.section]];
            self.currentModel = self.currentExpress[indexPath.row];
            self.currentExpressName = self.currentModel.name;
            self.currentExpressCode = self.currentModel.code;
    
            if (0 == indexPath.row) {
                [cell setName:self.currentExpressName andType:KCell_First];
            }else if (self.currentExpress.count -1 == indexPath.row){
                [cell setName:self.currentExpressName andType:KCell_Last];
            }else{
                [cell setName:self.currentExpressName andType:KCell_Other];
            }
    Cell
    -(void)setName:(NSString *)name andType:(NSString *)type{
        if ([@"isFirst" isEqualToString:type]) {
            UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, screenWidth-24,40) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5, 5)];
            CAShapeLayer* shape = [[CAShapeLayer alloc] init];
            [shape setPath:rounded.CGPath];
            
            self.baseView.layer.mask = shape;
        }else if([@"isLast" isEqualToString:type]){
            UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, screenWidth-24,40) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
            CAShapeLayer* shape = [[CAShapeLayer alloc] init];
            [shape setPath:rounded.CGPath];
            
            self.baseView.layer.mask = shape;
        }
        self.expressName.text = name;
    }
    

    tableView头容器

    头尾容器会和tableview一起动,而分组头不会动

    sendDetailsHeadView * headView = [[NSBundle mainBundle]loadNibNamed:@"sendDetailsHeadView" owner:nil options:nil].lastObject;
        headView.bounds = CGRectMake(0, 0, screenWidth-30, 270);
        [headView setWithHeadModel:self.headModel];
        self.sendDetailsTable.tableHeaderView = headView;
    

    代理方法

    1,设置代理 <UITableViewDelegate,,UITableViewDataSource>
    2,实现方法
    分组个数: numberOfSectionsInTableView
    每组个数 :numberOfRowsInSection
    加载 cell:cellForRowAtIndexPath
    1,设置 cell 的复用 id(在 cell 中设置)
    2,注册 id [self.detailTable registerNib:[UINib nibWithNibName:DailyPriceTimeCellID bundle:nil] forCellReuseIdentifier:DailyPriceTimeCellID];
    (这里 nib 的 name 作为 Identifier,nibname 不能错)
    3,复用DailyPriceTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:DailyPriceTimeCellID];
    [cell setUPModel:self.priceModel];
    return cell;
    设置分组头 :viewForHeaderInSection
    分组头的高度:heightForHeaderInSection
    设置每行的高度:heightForRowAtIndexPath
    非等高 cell 要先设置估计高度:estimatedHeightForRowAtIndexPath

    Cell按钮跳转

    点击 cell 中的按钮实现界面的跳转,要在 cell 写代理,点击按钮调用代理,在 cell 的控制器中调用实现的代理方法。

    -(void)goMyBuy
    {
    
        myBuyAndSellPage * myBuy = [[myBuyAndSellPage alloc] initWithAction:kBuyOrSell_Buy];
        myBuy.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:myBuy animated:YES];
    }
    

    长按cell删除

    //    手势
        UILongPressGestureRecognizer  *labelLongPressGestureRecognizer = [[UILongPressGestureRecognizer  alloc] initWithTarget:self
                                                                                                                        action:@selector(deleteCardClick:)];
        // 2. 将点击事件添加到label上
        labelLongPressGestureRecognizer.minimumPressDuration=1.5f;
        [cell addGestureRecognizer:labelLongPressGestureRecognizer];
    -(void)deleteCardClick:(UILongPressGestureRecognizer *)longRecognizer{
    
        if (longRecognizer.state == UIGestureRecognizerStateBegan) {//手势开始
            
            [self becomeFirstResponder];
            
            CGPoint location = [longRecognizer locationInView:self.cardBagTable];
            NSIndexPath * indexPath = [self.cardBagTable indexPathForRowAtPoint:location];
            //可以得到此时你点击的哪一行
            
            self.selectThirdModel = self.cardBag[indexPath.row];
            
        }
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                           message:@"删除卡包"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self canDeleteCard];
                                                                  }];
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                                 handler:^(UIAlertAction * action) {
                                                                     //响应事件
                                                                     
                                                                 }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
    }
    

    当超过tableView显示的范围的时候 后面显示的内容将会和前面重复

    self.selectCellRow 记录当前选中的cell,只有刷新到选中cell的时候,才改到选中的状态

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    FirstCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstCategoryCellID];
            cell.leftLine.hidden = YES;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (indexPath.row == self.selectCellRow) {
                cell.leftLine.hidden = NO;
            }
            [cell setUpModel:self.fModels[indexPath.row]];
            return cell;
    }
    

    下拉放大脑壳

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        //往上滑动offset增加,往下滑动,yoffset减小
        CGFloat yoffset = scrollView.contentOffset.y;
        if (yoffset<headerHeight) {//滑动到导航栏地底部之前
            
        }else {//超过导航栏底部
        }
        //处理放大效果和往上移动的效果
        if (yoffset>0) {//往上滑动
            
            self.headerView.frame = ({
                CGRect frame = originFrame;
                frame.origin.y = originFrame.origin.y - yoffset;
                frame;
            });
            
        }else {//往下滑动,放大处理
            self.headerView.frame = ({
                CGRect frame = originFrame;
                frame.size.height = originFrame.size.height - yoffset;
                frame.size.width = frame.size.height/scale;
                frame.origin.x = originFrame.origin.x - (frame.size.width-originFrame.size.width)/2;
                frame;
            });
            
        }
    }
    

    滚动到指定位置

    滚动到指定位置

    [self.SecondCategoryTable scrollToRow:0 inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
    

    滚动到指定位置并选中

    [self.adressTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
    

    相关文章

      网友评论

          本文标题:iOS - UITableView

          本文链接:https://www.haomeiwen.com/subject/frazbftx.html