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

相关文章

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字