iOS之UITableViewController使用详解(一)tableview上移

 

tableview上移解决:

self.edgesForExtendedLayout=UIRectEdgeNone;

if (@available(iOS 11.0, *)) {

[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];

}

//解决MJRefresh上下拉刷新无法收回

if (@available(iOS 11.0, *)) {
self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
if ([self.scrollView isKindOfClass:[UITableView class]]) {
// iOS 11的tableView自动算高默认自动开启,不想使用则要这样关闭
UITableView *tableView = (UITableView *)self.scrollView;
tableView.estimatedRowHeight = 0;
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;
}
} else {
// Fallback on earlier versions
self.automaticallyAdjustsScrollViewInsets = NO;
}

 

UITableView的多组显示

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

//tintColor 很多控件都有

    // 设置tableView的索引标题颜色

    //self.tableView.sectionIndexColor = [UIColor redColor];

  

    // 设置tableView的索引标题背景颜色 

//self.tableView.sectionIndexBackgroundColor= [UIColor redColor];

   

    // 设置tableView的索引标题选中时的颜色  

//self.tableView.sectionIndexTrackingBackgroundColor= [UIColor redColor];

    

    // 设置主题颜色 tintColor

    self.tableView.tintColor =HMColor(21,188,173);

     }

 

// 有多少组在tableView中

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    

    return 3;

}

 

// 每一组有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    /**

     因为每一组显示的行数不一样,所以要对每一组进行判断

     */

    NSInteger rows = 0;

    if (section == 0) {

        rows = 30;

    } else if (section ==1) {

        rows = 20;

    } else {

        rows = 10;

    }

 

    return rows;

}

 

// 每一行显示的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 实例化cell

    UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];

    

   /**

    indexPath.row    行

    indexPath.section  组

    定义唯一的一行

    

    */

    // 把组取出来

    NSInteger sectionIndex = indexPath.section;

    

    if (sectionIndex == 0) { // 0

        // 判断第0组的第几行

        NSInteger rowIndex = indexPath.row;

        

        if (rowIndex == 0) {

            cell.textLabel.text =@"火影:鸣人";

        } else if (rowIndex ==1) {

            cell.textLabel.text =@"上忍:卡卡西";

        } else {

            cell.textLabel.text =@"中忍:";

        }

    } else if (sectionIndex ==1) {

        // 两行

        

        if (indexPath.row ==0) {

            cell.textLabel.text =@"风影:我爱罗";

        } else {

            cell.textLabel.text =@"堪九郎";

        }

    } else {

        cell.textLabel.text  =@"小魔仙";

    }

    

    cell.separatorInset=UIEdgeInsetsZero;//cell的分割线不锁进

 

    return cell;

}

 

/**

 titleForHeaderInSection: 组的头部显示的文本

 */

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    

    // 判断是第几组,然后显示对应的文本

    NSString *title = @"";

    

    switch (section) {

        case 0:

        {

            title = @"火之国";

        }

            break;

        case 1:

        {

            title = @"风之国";

        }

            break;

        case 2:

        {

            title = @"巴拉巴拉";

        }

            break;

            

        default:

            break;

    }

    

    

    

    return title;

    

}

 

/**

 titleForFooterInSection: 组的头部显示的文本

 */

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

    // 判断是第几组,然后显示对应的文本

    NSString *title = @"";

    

    switch (section) {

        case 0:

        {

            title = @"五影最强";

        }

            break;

        case 1:

        {

            title = @"黑眼圈";

        }

            break;

        case 2:

        {

            title = @"巴拉巴拉小魔仙";

        }

            break;

            

        default:

            break;

    }

    

    

    

    return title;

}

 

- (BOOL)prefersStatusBarHidden {

    return YES;

}

 

@end

 

// 设置控制器成为tableView的代理

    _tableView.delegate =self;

    

    // 设置分割线颜色的

    _tableView.separatorColor = [UIColorredColor];

    

    // 侵蚀 , 分割线样式

    _tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLine;

    

    // top, left, bottom, right , 上, 下 是没有效果的

    _tableView.separatorInset =UIEdgeInsetsMake(0,0,0,0);

    

    // 允许多选

    _tableView.allowsMultipleSelection =YES;

    

    /**

     如果让tableiew自动的计算行高,那么就必须给他一个预估的行高

     

     如果设置了预估行高,那么tableview在去加载数据的时候,不会频繁的调用 heightForRowAtIndexPath:

     

     先调用一次 cellForRowAtIndexPath:

     再调用一次 heightForRowAtIndexPath:

     */

    self.tableView.estimatedRowHeight =10;

    

    // 让UITableView 自动的去计算cell的高度

    self.tableView.rowHeight =UITableViewAutomaticDimension

    // 设置行高 ,(静态设置)如果每个cell的高度都一样,推荐这种设置

//    _tableView.rowHeight = 100;

    

    //设置tableView的头

    UIView *headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,100)];

    [headerView setBackgroundColor:[UIColororangeColor]];

    

    _tableView.tableHeaderView = headerView;

    

    //设置tableView的尾

 

    UIView *footerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,100)];

    [footerView setBackgroundColor:[UIColoryellowColor]];

    

    _tableView.tableFooterView = footerView;

//设置cell的样式(系统自带的)

 

    UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];

    /**

     UITableViewCellStyleDefault : 不显示detailTextLabel

     UITableViewCellStyleValue1 : detailLabel 显示在 textLabel 右侧

     UITableViewCellStyleValue2 : imageView不再显示, textLabel居左变蓝色

     UITableViewCellStyleSubtitle :都显示, detailLabel 在 textLabel下侧

     */

 

// 设置cell上控件的内容

    HeroModel *model = self.dataArray[indexPath.row];

    

    // 设置imageView

    cell.imageView.image = [UIImageimageNamed:model.icon];

    

    // 设置文本

    cell.textLabel.text = model.name;

    

    // 设置detailTextLabel

    cell.detailTextLabel.text = model.intro;

    

    // 设置右侧箭头

    // accessory : 配件

    cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

    

    // 设置选择样式

    /**

     UITableViewCellSelectionStyleNone,

     UITableViewCellSelectionStyleBlue,  用灰色来代替了

     UITableViewCellSelectionStyleGray,

     UITableViewCellSelectionStyleDefault

     cell.selectionStyle = UITableViewCellSelectionStyleBlue;

     */

    

    /**

     设置选中的背景view

     UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];

     tempView.backgroundColor = [UIColor yellowColor];

     

     cell.selectedBackgroundView = tempView;

     */

    

    // cell.backgroundColor = [UIColor yellowColor];

    

    /**

     accessoryView 自定义控件

     自定义 accessoryView的时候, frame中的坐标(x,y)修改后无效

     UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

     tempView.backgroundColor = [UIColor redColor];

     

     cell.accessoryView = tempView;

     */

    

组的标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    

    return @"英雄";

}

 

// 可以对 section的header和 footer设置view

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    

    

    UIView *headerView = [[UIViewalloc]init];

    [headerView setBackgroundColor:[UIColorredColor]];

    

    return headerView;

}

 

组标题快速索引

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    

    return_indexArray;

    

}

 

// 滚动到最后一行

        [_tableViewscrollToRowAtIndexPath:indexPathatScrollPosition:UITableViewScrollPositionBottomanimated:YES];

 

#pragma mark -  返回cell行高的代理方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 要返回所有cell的行高

    // 取出contentFrameModel

    ContentFrameModel *frameModel = self.dataArray[indexPath.row];

    

    

    return frameModel.cellHeight;

}

 
======= 系统默认tableView是显示分隔线的,但不幸的是,如果我们cell过少的话,是会出现多余的分隔线的,其实要处理这个问题,很简单,

我们只需要在tableView加载在视图后,加上下面这一句代码:

swift:

 

self.tableView?.tableFooterView = UIView()

OC:

 

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值