iOS 系统控件UITableView使用之初级剑童篇(欢迎提建议和分享遇到的问题)

本文主要是介绍iOS 系统控件UITableView使用之初级剑童篇(欢迎提建议和分享遇到的问题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一.UITableView概述

 1.UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格(具体区别的话大家可以自行试验,区别还是蛮大,不过因为iOS7扁平化的效果,感觉没6显示的区别大):

        typedefNS_ENUM(NSInteger, UITableViewStyle) {

        UITableViewStylePlain,                 // regular table view

        UITableViewStyleGrouped                // preferences style table view

        };

     2.因为继承UIScrollView,因此也支持滚动,可以分区(组)显示内容,其中分区成为section,行成为row

     3.frame决定tableView显示的位置和边框,每一行的位置都会放一个UITableView负责显示行的内容

     4.style样式(参照第一条)   //   分割线样式 separatorStyle   //   分割线颜色 separatorColor   // 行高   rowHeight    //   控制代理  delegate    //   数据代理  dataSource  //  与边框的分隔距离设置 separatorInset


二.UITableView基本配置

     @主要是通过2个协议:UITableViewDataSourceUITableViewDelegate

1.dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和eordering),并根据用户的操作进行相应的数据更新操作,如果数据没有更具操作进行正确的更新,可能会导致显示异常,甚至crush。
2.delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];  (我的编辑环境都是支持ARC的)
    // 设置tableView的数据源  
    tableView.dataSource = self;  
    // 设置tableView的委托  
    tableView.delegate = self;  
    // 设置tableView的背景图  
    tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"humingtao.png"]];  

    @一个UIView中可以有多个UITableView,我们这里先举例就一个UITableView,所以配置时,没用到(UITableView *)tableView这个参数,直接返回值了

@protocol UITableViewDataSource<NSObject>

//  可选,默认是返回1,1个分区

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

    return 4;  // 这里将UITableView分成4个分区

}
//  下面2个方法都是完成配置必须实现的(引申,OC中的协议相当于JAVA中的接口,只不过,如果引用JAVA中的接口,则必须实现它的全部方法,在OC中则不用)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    
    // 设置每个section的row数量(都是从0下标开始)
    if (section == 0) {        
        return 10;
    }
    if (section == 1) {
        return 5;
    }
    if (section == 2) {
        return 5;
    }
    return 7;
}

//  用于设置每个row上面的cell,其中indexPath 索引路径-->两个属性:section and  row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // UITableViewCell的重用机制,将在下一章进行详细分析
    // cell的重用标识(*******************不是很懂,不知道是不是为了标识各种不同的cell)
    static NSString * cellIdentifier  = @"cell";
    // 从重用队列中取出cell对象
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    // 如果没有,则创建(解释:一般刚进入界面的时候,是不需要重用的,当时显示的是能够映入界面的足够的cell,只有拖动的时候,才需要)
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellIdentifier];
    }
    
    if (indexPath.section == 1) {
        cell.imageView.image = [UIImage imageNamed:@"image1.png"];
    }else{
        cell.imageView.image = [UIImage imageNamed:@"image.png"];
    }
  
    if (indexPath.row == 1) {
        cell.textLabel.text = @"我是个小小小小菜鸟";
    }else{
        cell.textLabel.text = @"我是个大大大大傻逼";
    }


    return cell;
}

// section头的title(例如,通讯录不同姓名标识)

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [NSString stringWithFormat:@"%i",section+1];

}

// section尾段的title 
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    
    return @"失恋者联盟";

}

// section索引的title集合(例如,通讯录索引,帮助快速找到姓名)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return [NSArray arrayWithObjects:@"A", @"B",@"C",@"D",@"E",@"F",@"G",@"H",nil];

}

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

// 设置cell行高(因为参数是indexPath,所以可以设置不同section的行高,也能设置同一section不容row的行高)

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

      return 80;

}

// section头部的height

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

}

// section尾部的height

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

}

// section头部的view

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

}

//  section尾部的view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

}

//  cell的缩进级别
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

}


三.UITableViewCell(label,button等是添加在"contentView"属性上,切记)

1.系统提供的UITableView也包含了四种风格的布局,分别是:
typedef enum {
    UITableViewCellStyleDefault,
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,
    UITableViewCellStyleSubtitle
} UITableViewCellStyle;


2.下面我们看一下cell在正常状态下和编辑状态下的构成图:


3.cel的属性



其中,cell.accessoryType =  UITableViewCellAccessoryCheckmark                                 对应上图第三个标识

         cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator                   对应上图第一个标识

         cell.accessoryType =  UITableViewCellAccessoryDetailDisclosureButton            对应上图第二个标识

4.cell的一些控制方法

// 辅助button点击  

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

}

// cell将要被选中
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

// cell被选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];   // 选中cell后,高亮状态立马就消失

}

// cell将要被取消选中
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){

}

// cell被取消选中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){

}

// 选择cell滑动出现[delete]按钮

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    NSLog(@"删除");  
}  


@就是当往上滑动UITableview的时候广告条也跟着往上滑动,刚开始以为那个广告得单独定义一个scrollview,现在才知道uitableview有个 tableHeaderView 这个属性,我们只需要设置tableHeaderView 这个属性就可以 ,就可以实现广告条跟着滚动的,想实现点击关闭按钮后广告条消失 ,只需要将 tableHeaderView 设为 nil 即可  ,即 mytableview.tableHeaderView = nil;原来如此简单


// 根据indexPath获取cell对象

@- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; 


//获取tableview正在window上显示的cell的indexPath

@- (NSArray *)indexPathsForVisibleRows;


隐藏多余的分割线

- (void)_setExtraCellLineHidden:(UITableView *)tableView

{

    UIView *view =[ [UIView alloc]init];

    view.backgroundColor = [UIColor clearColor];

    [tableView setTableFooterView:view];

}


@根据indexPath定位tableView的位置

<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-position: initial initial; background-repeat: initial initial;"><span class="typ" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(43, 145, 175); background-color: transparent; background-position: initial initial; background-repeat: initial initial;">NSIndexPath</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">*</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"> indexPath </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">[</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(43, 145, 175); background-color: transparent; background-position: initial initial; background-repeat: initial initial;">NSIndexPath</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"> indexPathForRow</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">row inSection</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">section</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">];</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">[</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">_tableView scrollToRowAtIndexPath</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">indexPath atScrollPosition</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">:</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(43, 145, 175); background-color: transparent; background-position: initial initial; background-repeat: initial initial;">UITableViewScrollPositionTop</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"> animated</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">YES</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">];</span></code>

这篇关于iOS 系统控件UITableView使用之初级剑童篇(欢迎提建议和分享遇到的问题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1088521

相关文章

关于跨域无效的问题及解决(java后端方案)

《关于跨域无效的问题及解决(java后端方案)》:本文主要介绍关于跨域无效的问题及解决(java后端方案),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录通用后端跨域方法1、@CrossOrigin 注解2、springboot2.0 实现WebMvcConfig

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

Go语言中泄漏缓冲区的问题解决

《Go语言中泄漏缓冲区的问题解决》缓冲区是一种常见的数据结构,常被用于在不同的并发单元之间传递数据,然而,若缓冲区使用不当,就可能引发泄漏缓冲区问题,本文就来介绍一下问题的解决,感兴趣的可以了解一下... 目录引言泄漏缓冲区的基本概念代码示例:泄漏缓冲区的产生项目场景:Web 服务器中的请求缓冲场景描述代码

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Java死锁问题解决方案及示例详解

《Java死锁问题解决方案及示例详解》死锁是指两个或多个线程因争夺资源而相互等待,导致所有线程都无法继续执行的一种状态,本文给大家详细介绍了Java死锁问题解决方案详解及实践样例,需要的朋友可以参考下... 目录1、简述死锁的四个必要条件:2、死锁示例代码3、如何检测死锁?3.1 使用 jstack3.2

解决JSONField、JsonProperty不生效的问题

《解决JSONField、JsonProperty不生效的问题》:本文主要介绍解决JSONField、JsonProperty不生效的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录jsONField、JsonProperty不生效javascript问题排查总结JSONField

Go语言中Recover机制的使用

《Go语言中Recover机制的使用》Go语言的recover机制通过defer函数捕获panic,实现异常恢复与程序稳定性,具有一定的参考价值,感兴趣的可以了解一下... 目录引言Recover 的基本概念基本代码示例简单的 Recover 示例嵌套函数中的 Recover项目场景中的应用Web 服务器中

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Linux系统中的firewall-offline-cmd详解(收藏版)

《Linux系统中的firewall-offline-cmd详解(收藏版)》firewall-offline-cmd是firewalld的一个命令行工具,专门设计用于在没有运行firewalld服务的... 目录主要用途基本语法选项1. 状态管理2. 区域管理3. 服务管理4. 端口管理5. ICMP 阻断