ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

OC 基础 UITableView

2021-09-17 20:06:55  阅读:317  来源: 互联网

标签:return tableView OC self 基础 mark cell UITableView


一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。

下面介绍一下tableview的常用属性跟方法

常用代理

<UITableViewDelegate,UITableViewDataSource>

实例化对象跟设置宽高

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

添加对象

[self.view addSubview:self.tableView];

设置代理

self.tableView.delegate = self;

设置数据源

self.tableView.dataSource = self;

设置背景颜色

self.tableView.backgroundColor = [UIColor whiteColor];

是否显示竖轴滚动条 NO:显示 YES:不显示

self.tableView.showsVerticalScrollIndicator = NO;

是否显示横轴滚动条 NO:显示 YES:不显示

self.tableView.showsHorizontalScrollIndicator = NO;

不显示cell底部线条

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

设置底部view

self.tableView.tableFooterView = [[UIView alloc] init];

设置cell的行高

self.tableView.rowHeight = 40;

注册cell

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ID"];

社会头部view

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];

设置底部view

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];

设置头部高度

self.tableView.sectionHeaderHeight = CGFLOAT_MIN;

设置底部行高度

self.tableView.sectionFooterHeight = CGFLOAT_MIN;

预估行高

self.tableView.estimatedRowHeight = 0;

预估头部高度

self.tableView.estimatedSectionHeaderHeight = 0;

预估底部行高度

self.tableView.estimatedSectionFooterHeight = 0;

设置内间距

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

滚动到tableView的最底部

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

刷新指定的某一行

[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

刷新指定的某一行

注意这个方法执行修改某个模型的数据进行刷新列 如果删除该行不能刷新列 会蹦掉的

为解决出来刷新整个tableview抖动的问题 这个比较详细

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];

刷新指定的某一列

NSIndexSet *set=[NSIndexSet indexSetWithIndex:0];
[self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];

删除某一行 参考2

[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

删除整列 后刷新

[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

实现代理方法

#pragma mark - 分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 0;
}

#pragma mark - 设置每一组的头部所展示的view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}

#pragma mark - 设置头部文本
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    NSString *title = [[NSString alloc] init];
    switch (section) {
        case 1:
            title = @"只在一以下时间段内接收比分直播推送";
            break;
        default:
            break;
    }
    return title;
    
}

#pragma mark - 设置组的头部的高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

#pragma mark - 设置每一组的底部所展示的view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [[UIView alloc] init];
}

#pragma mark - 设置低部文本
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    
    NSString *title = [[NSString alloc] init];
    switch (section) {
        case 0:
            title = @"开启后,当我投注或关注的比赛开始,进球和结束时,会自动发送推送消息提醒我";
            break;
        default:
            break;
    }
    return title;
    
}

#pragma mark - 设置组的低部的高度
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50;
}

#pragma mark - 设置行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

#pragma mark - 设置行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

#pragma mark - cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    return cell;
}


#pragma mark - 点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
}

#pragma mark - 显示右侧 组导航

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    //到模型中自动寻找title的值返回存到数组中
    return [self.carGroups valueForKeyPath:@"title"];
}

#pragma mark - 当点击右侧出现的删除按钮时触发–同时实现这个方法,就会在右侧出现删除按钮

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //NSLog(@"commitEditingStyle");
    //删除对应的模型
    [self.heros removeObjectAtIndex:indexPath.row];
    //刷新
    //[self.tableView reloadData];
    //将当前行从tableVIew中移除
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark - 设置右侧删除按钮的显示文本

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

自定义TableViewCell

.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface TableViewCell : UITableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView;

@end

.m

#import "TableViewCell.h"

@interface TableViewCell ()

@end

@implementation TableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
  static NSString *identifier = @"k<#cell当前类型#>ID";
  TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  if (cell == nil) {
    cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  }
  return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    [self buildUI];
  }
  return self;
}

- (void)buildUI{
  self.backgroundColor = [UIColor whiteColor];
  self.selectionStyle = UITableViewCellSelectionStyleNone;
  
}

@end

Xib

//2.读取指定的xib文件
UITableViewCell *cell=[[[NSBundle mainBundle] loadNibNamed:@"ID" owner:nil options:nil] lastObject];

//Xib加载完毕之后执行的方法(可以自定义控件)
- (void)awakeFromNib {
    //调用父类方法
    [super awakeFromNib];
    
}

标签:return,tableView,OC,self,基础,mark,cell,UITableView
来源: https://blog.csdn.net/weixin_38716347/article/details/120355468

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有