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

2024-05-31 11:18

本文主要是介绍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];

 

这篇关于iOS之UITableViewController使用详解(一)tableview上移的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1017800

相关文章

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

Apache伪静态(Rewrite).htaccess文件详解与配置技巧

《Apache伪静态(Rewrite).htaccess文件详解与配置技巧》Apache伪静态(Rewrite).htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令,主要的... 一、.htAccess的基本作用.htaccess是一个纯文本文件,它里面存放着Apache服务器

Python中conda虚拟环境创建及使用小结

《Python中conda虚拟环境创建及使用小结》本文主要介绍了Python中conda虚拟环境创建及使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录0.前言1.Miniconda安装2.conda本地基本操作3.创建conda虚拟环境4.激活c

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解