UITableView的使用总结
2024-09-05 10:23:11
直接贴代码了,很好理解,注释很全,一看就懂......
//
// ViewController.m
// TableViewSectionTitleDemo
//
// Created by 思 彭 on 2017/4/13.
// Copyright © 2017年 思 彭. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self setUI];
} #pragma mark - 设置界面 - (void)setUI { // 注意: 如果为group类型的tableView,会造成第一section的header的title高度有问题
/*
1.UITableViewStylePlain有多段时 段头停留(自带效果)
没有中间的间距和头部间距(要想有的重写UITableViewCell \UITableViewHeaderFooterView里面的setFrame方法)
2.UITableViewStyleGrouped
去掉头部和中间间隔
设置标头的高度为特小值 (不能为零 为零的话苹果会取默认值就无法消除头部间距了)
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
view.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = view;
*/ self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.tableView.tableFooterView = [[UIView alloc]init];
self.tableView.sectionIndexColor = [UIColor grayColor];
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
self.tableView.sectionHeaderHeight = 30.0f;
self.tableView.sectionFooterHeight = FLT_EPSILON; // 方法一:修改sectionHeader的背景颜色
// [[UITableViewHeaderFooterView appearance] setTintColor:[UIColor lightGrayColor]];
// 注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self.view addSubview: self.tableView];
} #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont systemFontOfSize:];
cell.textLabel.text = @"思思";
return cell;
} #pragma mark - UITableViewDelegate - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"A";
} - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView { return @[@"A", @"B", @"C", @"D", @"E"];
} - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return ;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } // 6.0以上使用
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// 方法二:修改sectionHeader的背景颜色 Background color
// view.tintColor = [UIColor blackColor]; // Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
header.backgroundColor = [UIColor redColor]; // 方法三:修改sectionHeader的背景颜色
header.contentView.backgroundColor = [UIColor lightGrayColor];
// Another way to set the background color
// Note: does not preserve gradient effect of original header
// header.contentView.backgroundColor = [UIColor blackColor];
} /* 方法四:修改sectionHeader的背景颜色
-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
UIView* content = castView.contentView;
UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
content.backgroundColor = color;
}
}
*/ // 让段头不停留(取消粘性效果)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = ;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, , , );
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, , , );
}
} @end
最新文章
- HTML5_06之拖放API、Worker线程、Storage存储
- DLUTOJ1216
- iPad 2升级iOS 9的过程记录
- Remove Duplicates from Sorted List I &; II
- winPcap_4_获取已安装设备的高级信息
- Nmon 性能:分析 AIX 和 Linux 性能的免费工具
- js 调用父窗口的方法
- 设计模式-GoF23
- sscanf( )函数初体验
- js面向对象学习笔记(五):tab切换
- [Noi2015]荷马史诗
- iframe中 父页面和子页面查找元素的方法
- 【原创】那些年用过的Redis集群架构(含面试解析)
- MySQL 数据查询
- 剑指offer面试题27:二叉搜索树与双向链表
- in exists
- 按字典序依次打印只由1~n组成的n位数
- 兼容多种模块规范(AMD,CMD,Node)的代码
- C#语言————第三章 使用属性升级MyBank
- django DateTimeField 时间格式化