美文网首页
UITableView的一些小方法

UITableView的一些小方法

作者: hAo_JS | 来源:发表于2016-11-11 13:44 被阅读0次
    1、UITableView设置偏移量

    通过设置tableView的偏移量,让列表默认滚动到某个位置。

    [myTableView setContentOffset:CGPointMake(0, 100) animated:YES];  
    
    2、刷新某行cell的方法

    有时候只需要刷新某行的cell的数据,完全没必要调用[tableView
    reloadData]刷新整个列表的数据,调用以下方法即可。

     NSIndexPath *indexPath_1=[NSIndexPath indexPathForRow:1 inSection:0];  
    
     NSArray *indexArray=[NSArray  arrayWithObject:indexPath_1];  
    
     [myTableView  reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic]; 
    
    
    3、改变分组列表之间的间距

    方法一

    改变每一组之间的间距

     self.tableView.sectionHeaderHeight = 5;  
     self.tableView.sectionFooterHeight = 0;  
    

    方法二

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
    
    {  
    
        return 5;  
    
    }  
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  
    
    {  
    
        return 5;  
    
    }  
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
    
    {  
    
        UIView *view = [[UIView alloc] initWithFrame:CGRectZero];  
    
        return view;  
    
    }  
    
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section  
    
    {  
    
        UIView *view = [[UIView alloc] initWithFrame:CGRectZero];  
    
        return view;  
    
    }  
    
    
    4、设置分割线的一些方法

    //设置分割线为蓝色
    
     [myTableview setSeparatorColor:[UIColor blueColor]];  
    
    //隐藏UITableViewCell的分隔线
    
    [self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
    
    // UITableViewCellSeparatorStyle有如下几种 
    
    typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
        UITableViewCellSeparatorStyleNone,
        UITableViewCellSeparatorStyleSingleLine,
        UITableViewCellSeparatorStyleSingleLineEtched  // This separator style is only supported for grouped style table views currently
    };
    
    //设置分割线的偏移量
    
        //分割线向右偏移100点
        myTableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);
    
    
    
    
    
    5、设置tableview的cell颜色

    //方法一:  
    
    cell.contentView.backgroundColor = [UIColor redColor]; 
    
    //方法二:  
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    
    UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];  
    
    bgview.opaque = YES;  
    
    bgview.backgroundColor = [UIColor orangeColor];  
    
    [cell setBackgroundView:bgview];  
    
    //方法三:  
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
    
    {  
    
    cell.backgroundColor = [UIColor redColor];   
    
    }  
    
    
    6、调整cell之间的距离

    #pragma mark 重写setFrame方法,自己调整cell的frame  
    
    - (void)setFrame:(CGRect)frame  
    
    {  
    
        // tableView边框的宽度 #define kTableBorderWidth 5  
    
        // 更改x、宽度  
    
        frame.origin.x = kTableBorderWidth;  
    
        frame.size.width -= kTableBorderWidth * 2;  
    
    
        // 更改顶部间距、每个cell之间的间距  
    
        frame.origin.y += kTableTopBorderWidth;  
    
        frame.size.height -= kTableViewCellMargin;  
    
        [super setFrame:frame];  
    
    }  
    
    7、cell选中和不选中显示不同背景

    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"choose_item_selected.jpg"]];  
    
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"choose_item.jpg"]];
    
    8、点击某个Cell无效

    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       //第一个cell 点击的时候没有反应
       if (indexPath.row ==0) {
           return nil;
       }
       return indexPath;
    }
    
    
    9、默认选中 TableView Cell的第一行

     NSIndexPath *path=[NSIndexPath indexPathForItem:0 inSection:0];
     [myTableView selecRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionButtom];
    
    10、选中 TableView Cell时改变字体颜色

     label.highlightedTextColor=[xxxxxx];
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    相关文章

      网友评论

          本文标题:UITableView的一些小方法

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