UITableView自定义TableHeader和TableFooter

news/2024/7/20 23:13:16 标签: xcode, ios, macos

UITableView自定义TableHeader和TableFooter

    • 我猜你希望的效果是这样的

我猜你希望的效果是这样的

在这里插入图片描述

自定义页眉视图
让我们创建一个文件名 UITableViewHeaderFooterView 的 CustomerHeaderView 子类。
在这里插入图片描述
现在让我们创建视图的 Xib 文件并将其命名为 CustomHeaderView。

在这里插入图片描述

更改高度标题视图在创建的 XIB 文件中选择视图。在属性检查器中选择大小并将其大小设置为自由度

在这里插入图片描述

自定义页脚视图 让我们创建一个文件名 UITableViewHeaderFooterView 的客户页脚视图
子类并重复上述步骤。

在主类文件中,寄存器头视图和页脚视图在视图中的表视图DidLoad

class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "CustomHeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "CustomHeaderView")
tableView.register(UINib(nibName: "CustomFooterView", bundle: nil), forHeaderFooterViewReuseIdentifier: "CustomFooterView")
  }
}
 
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let indentifier = "CustomHeaderView"
        let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: indentifier)
        if section == 0 {
            cell?.contentView.backgroundColor = .cyan
        }
        if section == 1 {
            cell?.contentView.backgroundColor = .red
        }
       
   
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let indentifier = "CustomHeaderView"
        let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: indentifier)
        cell?.contentView.backgroundColor = UIColor.tableSectionColor()
        
        return cell
    }
   
 
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
       
        
        
        return 100
    }
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
      
        
        return 50 
    }
    

http://www.niftyadmin.cn/n/4962122.html

相关文章

GDAL 图像分块处理操作

文章目录 一、简介二、实现代码三、实现效果参考资料一、简介 有时候图像太大,电脑的内存不足,我们无法将整个图像读取到内存中,那么此时就需要对图像进行分块处理了。GDAL为我们提供了分块的相关接口,这里以提取最大最小值为例来演示一下图像的分块操作。 二、实现代码 /…

vue3 02-reactive使用

1.reactive函数通常定义:复杂类型的响应式数据 2.reactive怎么使用&#xff1a; 2-1.首先先导入reactive函数 import { reactive } from vue;2-2.创建响应式数据对象const state reactive({name:老段,age:18})2-3.返回数据return{state}2-4.页面中展示<p>姓名:{{ state…

5.6 汇编语言:汇编高效数组寻址

数组和指针都是用来处理内存地址的操作&#xff0c;二者在C语言中可以互换使用。数组是相同数据类型的一组集合&#xff0c;这些数据在内存中是连续存储的&#xff0c;在C语言中可以定义一维、二维、甚至多维数组。多维数组在内存中也是连续存储的&#xff0c;只是数据的组织方…

Pyqt5-开源工具分解功能(配置文件+快捷写入)

开源第五篇,配置文件及参数配置,先来看个图: 上述是自动化电池监测的简图。会根据json文件中的数据从而自动写入数据。 如何自动写入数据 从GIF中可以看到,选中的输入的标签都是QLineEdit,而QLineEdit的写入文本方法是.setText(str),注意这里是写入的文本是text,字符串。…

c++ 枚举类

在C11及其后续版本中&#xff0c;引入了一个新的枚举类型称为“强类型枚举”或“枚举类”&#xff08;enumeration class&#xff09;&#xff0c;通常简称为“枚举类”&#xff08;enum class&#xff09;。 相对于传统的枚举类型&#xff0c;枚举类有以下特点和优点&#xf…

mysql 、sql server 临时表、表变量、

sql server 临时表 、表变量 mysql 临时表 创建临时表 create temporary table 表名 select 字段 [&#xff0c;字段2…&#xff0c;字段n] from 表

虚拟机 net、桥接、主机三种网络模式寻根问底

虚拟机使用物理主机上的网络适配器直接连接到物理网络中。 这意味着虚拟机就像是通过网线直接连接到路由器一样,成为物理网络中的一个独立设备。 虚拟机可以获取一个永久的IP地址,通过DHCP或手动设置。 虚拟机和物理主机都可以访问对方以及公共网络中的其他设备,比如文件服务…

C#,《小白学程序》第二课:数组与排序

1 文本格式 /// <summary> /// 《小白学程序》第二课&#xff1a;数组与排序 /// </summary> /// <param name"sender"></param> /// <param name"e"></param> private void button2_Click(object sender, EventArgs …