UITableVIew

UITableViewCell疊加 (clipsToBounds)UricRecord6Cell

image.png

如何從UITableViewCell獲取UITableView

if([self.superview isKindOfClass:UITableView.class]){
                    UITableView *tableView = (UITableView*)self.superview;
                    [tableView reloadData];
                }

UITableViewCell嵌套UICollectionView自適應高度
http://www.itdecent.cn/p/4bdd9e87504a

空cell,高度為0時

if(indexPath.row == 1 && [MJManager getUMIState:self.model] != 2){
                UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"];
                return cell;
            }

初始化


@property (nonatomic, strong) UITableView *tbvData;
- (UITableView *)tbvData
{
    if (!_tbvData)
    {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = kBgColor;
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tbvData = tableView;
    }
    return _tbvData;
}

 [self.view addSubview:self.tbvData];
 [self.tbvData mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
 }];

UITableViewDelegate,UITableViewDataSource

自定義cell,分區(qū)頭部,尾部

IB
UINib *cellNib = [UINib nibWithNibName:@"MineSimpleTitleTbvCell" bundle:nil];
[tableView registerNib:cellNib forCellReuseIdentifier:@"MineSimpleTitleTbvCell"];
UINib *sectionHeadNib = [UINib nibWithNibName:@"MyCustomersChildTbvHeadView" bundle:nil];
[tableView registerNib:sectionHeadNib forHeaderFooterViewReuseIdentifier:@"MyCustomersChildTbvHeadView"];

代碼
[tableView registerClass:[YZGTableVIewCell class] forCellReuseIdentifier:@"YZGTableVIewCell"];
[tableView registerClass:[YZGSectionHeadView class] forHeaderFooterViewReuseIdentifier:@"YZGSectionHeadView"];


下拉刷新

_tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    
 }];
_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

}];

_tableView.mj_footer.hidden = YES;
[__weakSelf.tableView.mj_header endRefreshing];
[__weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];//設置沒有更多數(shù)據(jù)

UITableViewDelegate,UITableViewDataSource

表頭,表尾

@property (nonatomic, strong) MJTableHeadView *tbvHeadView;
@property (nonatomic, strong) MJTableFootView *tbvFootView;

IB
- (AddWeiXinTbvFootView *)tbvFootView {
    if (!_tbvFootView) {
        _tbvFootView = (AddWeiXinTbvFootView *)[NSBundle.mainBundle loadNibNamed:@"AddWeiXinTbvFootView" owner:self options:nil].lastObject;
        _tbvFootView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 130);
        _tbvFootView.autoresizingMask = UIViewAutoresizingNone;
    }
    return _tbvFootView;
}
- (MyBalanceTbvHeadView *)tbvHeadView {
    if (!_tbvHeadView) {
        _tbvHeadView = (MyBalanceTbvHeadView *)[NSBundle.mainBundle loadNibNamed:@"MyBalanceTbvHeadView" owner:self options:nil].lastObject;
        _tbvHeadView.frame = CGRectMake(0, 0, SCREEN_WIDTH, AUTO_MATE_WIDTH(200));
        _tbvHeadView.autoresizingMask = UIViewAutoresizingNone;
    }
    return _tbvHeadView;
}

代碼 
- (MJTableFootView *)tbvFootView {
    if (!_tbvFootView) {
        _tbvFootView = [[MJTableFootView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    }
    return _tbvFootView;
}
- (MJTableHeadView *)tbvHeadView {
    if (!_tbvHeadView) {
        _tbvHeadView = [[MJTableHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    }
    return _tbvHeadView;
}

tableView.tableHeaderView = self.tbvHeadView;
tableView.tableFooterView = self.tbvFootView;

常用代理方法

#pragma mark ************** UITableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _dataArray.count;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIden = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cellIden];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//右邊尖括號
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//去掉點擊效果
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.cellClickBlack)
    {
        self.cellClickBlack(_dataArray[indexPath.row]);
    }
}

分區(qū)頭部,尾部

/*分區(qū)頭部部高度*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    YZGSectionHeadView *sectionHeadView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YZGSectionHeadView"];
 
    return sectionHeadView;
    
}

/*分區(qū)尾部高度*/
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    YZGSectionFootView *sectionFootView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YZGSectionFootView"];
    return sectionFootView;
}

UITableView 方法,特性

##默認選中第一行
NSIndexPath * path = [NSIndexPath indexPathForItem:0 inSection:0];
[self tableView:self.tbvData didSelectRowAtIndexPath:path];

##一句話去除UITableView底部多余行及分割線
 tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
##取消分割線
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
##分割線顏色
tableView.separatorColor = [UIColor redColor];
##右邊尖括號
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
##去掉點擊效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
##一個section刷新
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[self.tbvData reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
##一個cell刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[self.tbvData reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

##滾動到指定位置  
 NSIndexPath * dayOne = [NSIndexPath indexPathForRow:0 inSection:2];  
[self.tbvData scrollToRowAtIndexPath:dayOne atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
##滾動到頂部
 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
##cell 底部留空白
- (void)setFrame:(CGRect)frame
{
    frame.size.height -= 10;
    [super setFrame:frame];
}
##cell 數(shù)據(jù)少,直接 ,不需要復用
UITableViewCell *cell = [UITableViewCell alloc]init]
##iOS 11 設配  滑動卡頓(漂移和抽瘋的現(xiàn)象)3句不少
tableView.estimatedRowHeight = 0;
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;

##點擊松開灰色取消
[tableView deselectRowAtIndexPath:indexPath animated:true];

##取消向下偏移 64/20
if (@available(iOS 11.0, *)) {
        self.tbvData.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
##禁止滑動
tableView.scrollEnabled = NO;

##分割線邊距
tableView.separatorInset= UIEdgeInsetsMake(0,55, 0, 0);

##tableHeadView 用xib 時候用,高度會出錯
 tableView.autoresizingMask = UIViewAutoresizingNone;

##注意cell 約束的內(nèi)邊距
tableView.contentInset = UIEdgeInsetsMake(0, 0, 15, 0);
## sectionView 有默認顏色,xib 不修改他的 顏色,添加一個view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    UIView *sectionHeadView = [[UIView alloc]init];
    sectionHeadView.backgroundColor = tableView.backgroundColor;
    return sectionHeadView;
}
#點擊顏色修改
cell.selectedBackgroundView=[[UIView alloc]initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor= kBgColor;
#刷新后置頂
if(self.pageNumber == 1 && self.dataArray.count > 0){
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tbvData scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
    }
#TableViewCell 存UITableView 并自動計算高度
Outpatient 文件夾
F38E51B3-C589-4048-8B6C-328CBF5CCEC9.png

遇到的一些坑

###UITableView列表reloadSections等刷新數(shù)據(jù)時屏幕跳動
[UIView performWithoutAnimation:^{
        NSIndexSet *reloadSet = [NSIndexSet indexSetWithIndex:1];
        [self.workTable reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationNone];
}];
————————————————
版權聲明:本文為CSDN博主「番薯大佬」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/potato512/article/details/81047441
###隱藏某個cell 時候,高度為0時候,可能出現(xiàn)約束沖突,切換普通cell
UITableViewCellStyleValue1 搜索
###UITableViewStyleGroup 的 heightForFooterInSection 默認都是 22
設置 分區(qū) 頭尾 為 0 無效
設置UITableViewStyleGroup 時候,要讓分區(qū)尾部為0
(重要*************這里有一個小小的技巧,我們設置 0.01 的時候,這個時候?qū)ITableVIew是有作用的,*****不能設置為0,不然沒有效果,);

###button 添加在 cell 上,當button 設置不可點擊時,點擊button 時會觸發(fā)cell 的點擊事件,
現(xiàn)在讓button 不可點擊轉(zhuǎn)態(tài),點擊button 不會觸發(fā)cell 的點擊事件
(可以給 讓button 添加在 cell 的 一個 bottomVIew 上,給父視圖添加一個空的手勢點擊事件,阻止事件chuan'd)

設置tableView右側(cè)索引
http://www.itdecent.cn/p/2b87b09488e6
UITableview 添加索引后整體內(nèi)容左移
http://www.itdecent.cn/p/8a46869b18b2
tableViewCell里嵌套collectionView
http://www.hangge.com/blog/cache/detail_1591.html
http://www.itdecent.cn/p/b907c198473d
UITableView自動計算cell高度
https://github.com/forkingdog/UITableView-FDTemplateLayoutCell
http://www.itdecent.cn/p/64f0e1557562
http://www.itdecent.cn/p/af4bc69839d8
UITableView嵌套 UIScrollView
http://www.itdecent.cn/p/8bf6c2953da3
UITableView 優(yōu)化
https://blog.csdn.net/hmh007/article/details/54907560
UITableView 左滑刪除
http://www.itdecent.cn/p/2f37d553edb0
tableViewCell圖片點擊放大縮小回原位
https://blog.csdn.net/hero_wqb/article/details/50909765
TableViewCell注冊和不注冊復用的問題
http://www.itdecent.cn/p/12beb11ae63a
UITableViewcell分割線相關、隱藏某條分割線
https://blog.csdn.net/ZHFDBK/article/details/79411351
解決iOS UITableView分組header懸浮,每個section header上面有一段空白間距
https://blog.csdn.net/smile82475/article/details/125187582

UITableView 的總結(jié)

http://www.itdecent.cn/p/a5f6c534695e
http://www.itdecent.cn/p/344ffc33a435
http://www.itdecent.cn/p/481da2f1bf7a
http://www.itdecent.cn/p/7e6b5c6c6913

拓展

willDisplayCell 和cellForRowAtIndexPath區(qū)別

UITableViewCell 嵌套 UICollectionView
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容