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

2023-05-16

 

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];

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS之UITableViewController使用详解(一)tableview上移 的相关文章

  • 获取对 iOS 应用程序中最顶层视图/窗口的引用

    我正在创建一个可重用的框架 用于在 iOS 应用程序中显示通知 我希望将通知视图添加到应用程序中其他所有内容的顶部 有点像 UIAlertView 当我初始化监听 NSNotification 事件并添加视图作为响应的管理器时 我需要获取对
  • 如何使用MKMapView完成加载委托,可能的“完成显示”委托?

    当用户在选择注释后点击 保存 时 我尝试保存地图视图的缩略图 当用户尚未放大该注释时会出现问题 因此尚未加载关闭缩放级别 这就是用户点击保存后我正在做的事情 将布尔值 saving 设置为 true 居中并放大注释 无动画 当调用mapVi
  • 无法将“SDWebImageActivityIndi​​cator”类型的值分配给“ST_SDWebImageIndicator”类型?

    I have multiple flavours targets in my Xcode project I am also using SDWebImage in my app Everything was working fine un
  • 使用 NSURLSessionDataTask 显示文件下载进度

    我想显示特定文件的文件下载进度 收到了多少字节 它与 NSURLSessionDownloadTask 配合得很好 我的问题是我想用 NSURLSessionDataTask 实现同样的效果 以下是将文件接收到 NSData 并写入文档文件
  • -[EAGLContext renderbufferStorage:fromDrawable:] 第二次失败?

    我正在开发一个 iOS openGL ES 应用程序 我正在做通常的 EAGLView ES2Render 的事情 启动时 使用以下代码成功创建 frambuffer BOOL createFramebuffers EAGLContext
  • 带有 Core Data 对象的动态 UITableView 高度

    过去几天我一直在试图解决一个谜团 即为什么我的批处理大小为 20 的 NSFetchedResultsController 总是在获取完成后立即错误 即加载到内存中 我的所有对象 从而导致请求需要约 20 秒 事实证明 这是因为在我的 he
  • GMSMapView 中的倒多边形

    我必须在我的 iPhone 项目中使用 Google 地图 并且我正在使用 GMSPolygon 来绘制多边形 但是如何填充地图上除多边形内部之外的所有位置 就像下图一样 谢谢 我玩过你的问题 主要思想是用多边形填充整个地球 然后为您的特定
  • 如何在 Swift 3 中解析 JSON 数组 [重复]

    这个问题在这里已经有答案了 我从 Socket 获取了一些我想访问的数据 但收到错误消息 指出每次都无法将 NSArray 转换为 NSDictionary struct SocketEventHandler let event Strin
  • 这个错误是无效上下文0x0吗?

    我在ViewDidLoad中编写了以下代码 Implement viewDidLoad to do additional setup after loading the view typically from a nib void view
  • Swift - 选择值后隐藏 pickerView

    我发现了类似的问题 他们的答案很有帮助 但我坚持最后一件事 我试图在点击字段时显示 pickerView 然后选择数据时 我希望 pickerView 隐藏 我可以从 pickerView 获取数据来隐藏 但是 pickerView 后面仍
  • Transit MKDirectionsRequest 产生 null 错误 Error Domain=MKErrorDomain Code=5 "(null)"

    我正在尝试使用 MapKit Directions Request 来获取两个坐标之间的交通方向 当我切换到其他 非 Transit 类型时 下面的代码可以工作 但是当我切换到 Transit 时 它会抛出一个错误 该错误在 Apple 文
  • Objective Flickr 照片上传错误

    我正在使用 ObjectiveFlickr 库将照片从我的 iPhone 应用程序上传到 Flickr 我可以授权该应用程序并执行一般请求 但在尝试上传照片时遇到错误 要上传的照片是使用 AVFoundation 捕获的图像 这是相关代码
  • 如何从代码隐藏中设置 CarouselView 的项目?

    我有一个 CarouselView 它绑定到图像的 ItemsSource 但我想通过更改 CarouselView 的索引来更改当前显示的图像 我尝试使用 CarouselView Position 作为必须选择的元素的索引 但不幸的是这
  • iOS UIButton 带有圆角和背景 bug

    我发现圆形 UIButton 存在一个奇怪的问题 这是我创建此按钮的代码块 let roundedButton UIButton type System roundedButton frame CGRectMake 100 100 100
  • 我可以知道 requireGestureRecognizerToFail 到底会做什么吗?

    谁能告诉我下面的代码行到底会做什么 我已经提到过Apples https developer apple com library ios documentation uikit reference UIGestureRecognizer C
  • 自定义 MKAnnotationView - 如何捕获触摸而不忽略标注?

    我有一个自定义 MKAnnotationView 子类 它完全按照我想要的方式显示视图 在那个视图中 我有一个按钮 我想捕获按钮上的事件来执行操作 这很好用 但是 我不希望标注被忽略或消失 基本上 触摸标注中的按钮将开始播放声音 但我想保留
  • Swift 中通过不同类调用委托方法

    我正在获取 JSON 菜单 一旦 JSON 返回 我想运行 menuReady 来更新表的内容在 SomeTableViewController 类中 但下面的代码似乎不起作用 AIM Run 菜单就绪 JSON 返回后更新内容 PROBL
  • 将 iPhone 上的 stderr 写入文件和控制台

    我正在遵循答案中的建议here https stackoverflow com questions 5179108 iphone how to read application logs from device用于将 iOS 设备上的 NS
  • NSPredicate 的 onFormat 字符串

    我想用 id 键对数据进行排序 我如何理解格式字符串的用途NSPredicate格式 我有一个100号的帖子 我的代码 let objectIDs posts map 0 id let predicate NSPredicate forma
  • 在 iOS 上从 GPS 获取时间

    我正在开发一个跟踪器应用程序 该应用程序需要高精度地了解设备位置 即它使用位置服务并忽略水平精度低于 20 米的位置 CLLocation没有明确声明是否通过 GPS 确定 但是 如果水平精度为 20 米或更好 则可以认为它是来自 GPS

随机推荐

  • Java问题_直接运行jar文件,系统没有反应

    问题 xff1a 直接运行jar文件 xff0c 系统没有反应 问题分析 xff1a 我比较好奇为什么安装了Java就可以直接运行 jar文件 https blog csdn net walkingmanc article details
  • 虚拟机安装archlinux的简单步骤

    这篇文章是我在虚拟机安装archlinux之后的一点心得 xff0c 参考了archwiki关于安装arch的一些内容https wiki archlinux org index php Beginners 27 guide 准备工作 选择
  • 服务器远程连接经常连接不上的解决方法

    我们大家在使用服务器的时候或多或少都会有碰上服务器突然远程不上的情况这边给大家分享的服务器远程不上解决办法 xff01 第一步 xff1a 机器如果不通看下是不是被牵引或者内网牵引 xff0c 打开kvm查看系统是否正常运行 1 远程端口通
  • webservice的基础知识以及入门案例1

    一 webService 1 1 webservcie webservice是一种跨操作系统和跨语言的数据调用 数据交换的一种服务技术 用于网络通信 xff0c 多台机器之间的数据交互 最大优点 xff1a webservice最大的好处是
  • 解决docker容器中文乱码,修改docker容器编码格式

    原文链接 http www cnblogs com z belief p 6148463 html 前台上传文件到服务器后 xff0c 服务器返回给前台的文件列表中出现中文乱码 xff0c 所有的中文文件名全部变成 xff1f xff0c
  • Debug: django model querysets-> pandas.Dataframe

    问题描述 在使用Django 的ORM model时 xff0c 有时需要将数据导出并转变成pandas DataFrame 正常的流程应该是 xff1a span class hljs keyword import span pandas
  • mysql数据导出与导入

    只导出表结构 不导出数据 mysqldump opt d stat uroot pxxxx gt stat struct sql linux下 一 导出数据库用mysqldump命令 xff08 注意mysql的安装路径 xff0c 即此命
  • consul配置

    配置文件 1 除了命令行选项之外 xff0c 配置还可以放入文件中 在某些情况下 xff0c 这可能更容易 xff0c 例如使用配置管理系统配置Consul时 2 配置文件是JSON格式 3 配置文件不仅用于设置代理 xff0c 还用于提供
  • consul配置ACL

    1 consul配置文件目录下新增配置文件acl json 内容如下 xff08 实际使用时 xff0c 将注释删除才可使用 xff09 34 acl datacenter 34 34 dc1 34 需要acl配置的数据中心 一般默认是dc
  • centos pptp搭建,windows网关配置

    目录 一 安装 二 配置 三 运行 四 防火墙 五 限速 六 常见问题 七 windows网关设置 一 安装 yum install ppp pptpd y 需要防火墙转发 xff0c 如不想使用iptables可使用其他防火墙 yum i
  • NGINX LOCATION规则

    语法规则 xff1a location 61 uri 首先匹配 61 xff0c 其次匹配 其次是按文件中顺序的正则匹配 xff0c 最后是交给 通用匹配 当有匹配成功时候 xff0c 停止匹配 xff0c 按当前匹配规则处理请求 符号 含
  • -bash:lsnrctl:command not found错误

    哎 xff0c 这个错误折腾了我一下午 xff0c 网上搜索了半天 xff0c 没一个用得上的 现在终于知道为什么了 在linux下 xff0c 使用切换用户命令su切换到ORACLE用户时要加 xff0c 否则ORACLE下的命令全都无效
  • Docker-compose+Dockerfile构建并启动php7.4镜像

    利用官方镜像 43 Dockerfile构建符合自己要求php7 4镜像 DockerFile apt官方源太慢时 xff0c 切换apt源该dockerfile支持的php额外扩展 bcmatch event exif gd mysqli
  • docker-compose快速部署pptp,用于办公环境

    由于pptp安装部署涉及到防火墙 xff0c 路由转发 xff0c 系统内核的相关问题较为复杂 xff0c 且难以排查 xff0c 此处直接使用docker compose快速部署pptp version 39 3 39 services
  • MySQL 手动主从同步不锁表

    本文只能保证锁表时间不会太久 可能会出现从库需要跳过很多语句的情况 备份主库 mysqldump skip lock tables single transaction flush logs hex blob uroot pXXXX mas
  • 实战踩坑---MFC---CreateEvent

    使用事件CreateEvent注意事项 HANDLECreateEvent LPSECURITY ATTRIBUTESlpEventAttributes 安全属性 BOOLbManualReset 复位方式 BOOLbInitialStat
  • ARM指令中如何判断一个立即数是有效立即数

    声明 xff1a 1 本文内容为本人学习嵌入式linux所遇问题后 xff0c 为方便以后学习查阅转载所得 xff0c 若能助人 xff0c 也算报答前人之恩 xff01 2 如若侵犯原创作者权益 xff0c 请与我联系 xff0c 本人愿
  • 8.C++中的拼接字符串

    用sprintf 函数将一个变量从int类型转换到字符串类型 为了正确地完成这个任务 xff0c 你必须确保证目标缓冲区有足够大空间以容纳转换完的字符串 此外 xff0c 还必须使用正确的格式化符 如果使用了不正确的格式化符 xff0c 会
  • 左闭右开的由来(比如python等的range)

    转自 xff1a https www jianshu com p 5eaa330788e8 为了表示 2 3 12 这样一个序列 xff0c 有四种方法 xff1a 2 i lt 13 xff08 左闭右开区间 xff09 1 lt i 1
  • iOS之UITableViewController使用详解(一)tableview上移

    tableview上移解决 xff1a self edgesForExtendedLayout 61 UIRectEdgeNone if 64 available iOS 11 0 UIScrollView appearance setCo