美文网首页
UITableView

UITableView

作者: DDY | 来源:发表于2016-11-02 17:23 被阅读134次

    UITableViewCell的四种类型

    typedef NS_ENUM(NSInteger, UITableViewCellStyle) {  
        UITableViewCellStyleDefault,   
        UITableViewCellStyleValue1,  
        UITableViewCellStyleValue2,   
        UITableViewCellStyleSubtitle 
    }; 
    

    实际应用

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell;
        switch (indexPath.row) {
            case 0:
            {
                cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL1];
                cell.backgroundColor = [UIColor yellowColor];
                cell.selectionStyle = UITableViewCellSelectionStyleDefault;
            }
                break;
            case 1:
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CELL2];
                cell.backgroundColor = [UIColor redColor];
                cell.selectionStyle = UITableViewCellSelectionStyleGray;
            }
                break;
            case 2:
            {
                cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CELL2];
                cell.backgroundColor = [UIColor blueColor];
                cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            }
                break;
            case 3:
            {
                cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CELL4];
                cell.backgroundColor = [UIColor purpleColor];
                cell.selectionStyle = UITableViewCellSelectionStyleDefault;
            }
                break;
        }
        cell.imageView.image = [UIImage imageNamed:@"warning_btn"];
        cell.detailTextLabel.text = @"detailTextLabel";
        cell.textLabel.text = @"textLabel";
        return cell;
    }
    

    效果图

    UItableViewCellStyle.png

    UITableViewCell的四种类型

    UITableViewCellAccessoryNone,
    UITableViewCellAccessoryDisclosureIndicator,
    UITableViewCellAccessoryDetailDisclosureButton,
    UITableViewCellAccessoryCheckmark
    UITableViewCellAccessoryDetailButton
    
    accessoryType.png

    获取button 所在cell行

    cell 上面放button, 除了通过 cell.btn.tag 值获取cell的行数之外,另一种方便可行的方式

    [cell.button addTarget:self action:@selector(cellButtonClicked:event:) forControlEvents:UIControlEventTouchUpInside];
    // 点击事件
    - (void)cellButtonClicked:(id)sender event:(id)event { 
    NSSet * touches =[event allTouches]; 
    UITouch * touch =[touches anyObject];
     CGPoint currentTouchPosition = [touch locationInView:_tableView]; 
    NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:currentTouchPosition]; 
    if (indexPath!= nil) { 
    // 代码区 
    }
    }
    

    防止点击cell后仍为灰色

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    防止plain模式悬浮

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        CGFloat sectionHeaderHeight = 50;
        if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
    

    改变HeaderFooter颜色

    #pragma mark 代理方法改变headerfooter颜色
    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
    {
        if ([view isMemberOfClass:[UITableViewHeaderFooterView class]])
        {
            ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor clearColor];
        }
    }
    

    手势冲突

    很多猿(特别新猿)为了实现某些功能(例如收键盘)就给表格或者表格父视图添加了手势,那么问题来了: gestureRecognizer会影响到tableview本身事件,导致代理方法不回调(例如didSelectRowAtIndexPath:不走)。
    

    解决方案:实现gestureRecognizer的一个代理

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        return ![NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"];
    }
    

    更多手势冲突

    使用中存在的误区

    手动调用 tableView:cellForRowAtIndexPath: 获取cell,引起的卡顿问题
    即使有经验的猿也会进的一个误区: 手动调用tableView:cellForRowAtIndexPath: , 例如在tableView: didSelectRowAtIndexPath: 或者 tableView:heightForRowAtIndexPath: 中 [tableView cellForRowAtIndexPath:indexpath]; 获取cell,来进行某些操作,但是这样手动调用改方法产生的cell就不再参与cell复用了,就直接导致在你浑然不知的情况下可能创建了很多cell,当界面单一时体现不明显,但是当有复杂cell且多图情况下,就造成了卡顿。
    解决方案:在继承UITableViewCell的自定义cell中改变你想要的属性,而不是在tableView中获取cell传值进入。
    

    注意事项

    • insert Row或者delete Row的时候要相应的增加和减少代理方法返回的行数。
    • 注册自定义cell都要在tableview(collectionview也适用)初始化之后,否则无效。单独xib加载文件后才能有效注册cell。
    • tableView的section头和section尾的高度设置为0时是无效的。
    // 只设一个可能仍出现20的高度,也不可设置0
    // Header底部间距
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
    {  
        return 40;//section头部高度  
    }  
    // footer底部间距  
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  
    {  
        return 0.001;  
    } 
    

    tableView各种模式(删除,移动,排序)
    iOS UITableView小记录

    相关文章

      网友评论

          本文标题:UITableView

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