MyTableView

XCode自带的Code Snippet Libray(代码仓库)是非常方便的工具,当然最近几个版本拖入有些问题,这里分享一下最常用的TableView Code,不断完善中勿喷


Code Snippet Library
Code Snippet Library
<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.estimatedRowHeight = 300.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.backgroundColor = [UIColor whiteColor];
}

- (UITableView *)tableView{
    if (!_tableView) {
        CGSize windowsRect = [UIScreen mainScreen].bounds.size;
        CGRect tableRect = CGRectMake(0, 0, windowsRect.width, windowsRect.height);
        _tableView = [[UITableView alloc]initWithFrame:tableRect style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = YES;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        //_tableView.separatorStyle = UITableViewCellAccessoryNone;
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
        [_tableView setSeparatorColor:RGB(240, 240, 240)];
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
        
        
        [self.view addSubview:_tableView];
        
        _tableView.mj_header.automaticallyChangeAlpha = YES;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self reFreshData];
        }];
        _tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            [self loadMore];
        }];
    }
    return _tableView;
}

- (void)reFreshData{
    
}

- (void)loadMore{
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return <#expression#>
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return <#expression#>
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"<#expression#>";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    return cell;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容