UITableView使用總結

一:tableView知識點

//設置分割線的樣式為無
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
          
//設置分割線的顏色
self.tableView.separatorColor = [UIColor redColor];
     
//設置分割線的上下左右,受影響的只有左右,
self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
     
//設置內邊距
self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
     
//有內容的有下劃線,沒內容的無下劃線
self.tableView.tableFooterView = [[UIView alloc]init];

// 當選中某行的時候觸發
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     //立刻讓當前行變為非選中
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//設定tableView具體某一行的高度,或者整體的每一行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     if (indexPath.row % 2 == 0) {
         return 100;
     }else{
         return 55;
     }
}

//iOS8之后的自適應高度,(cell的垂直布局上下要記得設置)
self.tableView.estimatedRowHeight = 44.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
     

二:cell的總結

//cell的frame從新布局  設置cell的尺寸,一般用來設置cell之間的分割線
 -(void)setFrame:(CGRect)frame{
     CGFloat margin = 10;
     frame.origin.x = margin;
     frame.size.width -= 2 * margin;
     frame.size.height -= margin;
     frame.origin.y += margin;
     [super setFrame:frame];
}

//初始化控件創建
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
      //創建控件
      self.selectionStyle = UITableViewCellSelectionStyleNone;
      self.contentView.backgroundColor = [UIColor whiteColor];
      self.backgroundColor = [UIColor whiteColor];
      self.layer.shouldRasterize = YES;
      self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    }
    return self;
}

//添加系統箭頭
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//設置cell的顏色
self.contentView.backgroundColor =  [UIColor redColor];

//讓用戶點擊cell不變色
 cell.selectionStyle =UITableViewCellSelectionStyleNone;
     
//設置每個cell的背景圖片
cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_apple_small"]];
     
//設置點擊cell時的顏色
UIView *clickView = [[UIView alloc]init];
clickView.backgroundColor = [UIColor colorWithRed:191.0/255.0 green:219.0/255.0 blue:229.0/255.0 alpha:0.3];
cell.selectedBackgroundView = clickView;

三:tableView的簡單使用

3-1:聲明重用標示符

//重用標示符
static NSString * const CellID = @"CellID";

3-2:注冊cell
此處不考慮xib,storyboard,作者風格偏純代碼

//注冊cell
[self.tableView registerClass:[WYCell class] forCellReuseIdentifier:CellID];

3-3:使用

//有多少個section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//section里有多少個cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.topics.count;
}
//賦值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RBCheckRegistrationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
    cell.invitationActivityModel = self.topics[indexPath.row];
    return cell;
}

四:UITaleView的Header和Footer使用,

4-0:為每個section的Header或者Footer添加標題,實現以下2個方法即可,不再需要其他的操作。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

4-1:如果你不滿意僅有標題的Header和Footer,可以為Header和Footer自定義View。
自定義Header的View,實現UITableViewDelegate的:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = WYRGBColor(214, 87, 76);

    //時間
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.text = @"時間";
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.font = DEF_SFont15;
    timeLabel.textColor = [UIColor whiteColor];
    [timeLabel sizeToFit];

    //添加控件
    [headView addSubview:timeLabel];

   //隨意設置下位置
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headView);
        make.left.equalTo(headView.mas_left).offset(padding);
    }];
    return headerView;
}

除了實現UITableViewDelegate的方法返回自定義View之外,必須實現UITableViewDelegate的方法,明確返回Header的高,才能使自定義的View顯示出來。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

4-2:自定義Footer的View,和上面實現的header一樣,方法如下

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView ...
    return footerView;
}

明確返回Footer的高,才能使自定義的View顯示

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
5:刪除cell
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 從數據源中刪除
    [self.dataArray removeObjectAtIndex:indexPath.row];
    // 從列表中刪除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

五:tableView頭部視圖拉伸放大

頭部拉伸.gif

1、三個不可缺少的屬性

//頭部View
@property(nonatomic,strong)UIView *headView;
//頭部圖片
@property(nonatomic,strong)UIImageView *headImageView;
//狀態欄注意事項
@property(nonatomic,assign)UIStatusBarStyle statStyle;
//圖片高度   
#define kHeadViewHeight 200

2、實現的關鍵方法

//創建tableView
-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellID];
    //設置tableView間距
    self.tableView.contentInset = UIEdgeInsetsMake(kHeadViewHeight, 0, 0, 0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(kHeadViewHeight, 0, 0, 0);
    [self.view addSubview:self.tableView];
}

-(void)initHeadView{
    //狀態欄初始化
    _statStyle = UIStatusBarStyleLightContent;
    //頭部視圖
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYScreenW, kHeadViewHeight)];
    self.headView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.headView];
    //頭部圖片
    self.headImageView = [[UIImageView alloc]initWithFrame:self.headView.bounds];
    self.headImageView.image = [UIImage imageNamed:@"headimage.jpg"];
    self.headImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.headImageView.clipsToBounds = YES;//圖片過大的裁切
    [self.headView addSubview:self.headImageView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsety = scrollView.contentOffset.y+scrollView.contentInset.top;
    NSLog(@"%f",offsety);
    if (offsety <= 0)
    {
        //放大
        CGRect frame= self.headView.frame;
        frame.origin.y = 0;
        
        frame.size.height = kHeadViewHeight - offsety;
        self.headView.frame = frame;
        
        frame = self.headImageView.frame;
        frame.size.height = self.headView.frame.size.height;
        self.headImageView.frame = frame;

        //導航欄的設置
        //設置透明度
        self.headImageView.alpha = 1.0;
        //根據透明度修改狀態欄顏色
        _statStyle = UIStatusBarStyleLightContent;
        //主動更新狀態欄
        [self.navigationController setNeedsStatusBarAppearanceUpdate];
        
    }else{
        //整體移動
        CGRect frame= self.headView.frame;
        //header的最小y值
//        CGFloat min = kHeadViewHeight - 64;
        CGFloat min = kHeadViewHeight;
        frame.origin.y = -MIN(min, offsety);
        
        frame.size.height = kHeadViewHeight;
        self.headView.frame = frame;
        
        frame = self.headImageView.frame;
        frame.size.height = self.headView.frame.size.height;
        self.headImageView.frame = frame;

        //導航欄的設置
        CGFloat progress = 1-(offsety/min);
        self.headImageView.alpha = progress;
        //根據透明度修改狀態欄顏色
        _statStyle = (progress < 0.5) ? UIStatusBarStyleDefault:UIStatusBarStyleLightContent;
        //主動更新狀態欄
        [self.navigationController setNeedsStatusBarAppearanceUpdate];
    }
}

//狀態欄方法
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

導航欄

勤學如早春之苗,不見其增,日有所漲。
輟學如磨刀之石,不見其減,日有所損。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容