准备数据:
通过Xcode生成数据源文件 NumberGroup.h NumberGroup.m;
.h文件为头文件 写声明
.m文件为源文件 写实现的方法
NumberGroup.h:
#import <Foundation/Foundation.h>
@interface NumberGroup : NSObject
@property (nonatomic,copy) NSString *groupHeader;
@property (nonatomic,copy) NSString *groupFooter;
@property (nonatomic,copy) NSString *groupIndex;
@property (nonatomic,strong) NSMutableArray *groupNumbers;
+(instancetype) numberGroup;
@end
@property一般可存放三种类型的属性,一般我们可以选择其中的一种或者即为@property(A,B,C)
a.第一种表示的是可读性
readonly:表示该变量只是可读,但是不能修改。
readwrite:表示变量的值既可读也可以修改。
b.第二种表示原子性
natomic:默认属性,在多线程情况下会生成一些互斥加锁代码,避免变量的读写不同步的问题。
nonatomic:非原子性访问,不加同步,多线程并发访问会提高性能。
c.第三种表示内存管理
assign:setter 方法直接赋值,引用计数器不会增加,为了解决原类型与环循引用问题,一般用于简单的数据类型int,float等就用它。
retain:使用reatin的话,赋值的时候引用计数器会+1,防止被内存释放掉。拷贝类型属于指针拷贝。
copy:使用copy的话,赋值的时候引用计数器会+1,防止被内存释放掉。拷贝类型属于内容拷贝。
strong:强引用,其存亡直接决定了所指对象的存亡。如果不存在指向一个对象的引用,并且此对象不再显示列表中,则此对象会被从内存中释放。简单讲strong等同于retain。
weak:弱引用,不决定对象的存亡。即使一个对象被持有无数个若引用,只要没有强引用指向他,那麽其还是会被清除。简单讲weak比assign多一个功能,就是当对象消失之后会自动把指针变为nil。
instancetype只能作为函数返回值,不能用来定义定义变量。
用instancetype作为函数返回值返回的是函数所在类型的类型。
NSMutableArray:可变数组 NSArray为不可变数组
NumberGroup.m:
#import "NumberGroup.h"
@implementation NumberGroup
+(instancetype) numberGroup {
NumberGroup *numberGroup = [[NumberGroup alloc]init];
//设置数据
numberGroup.groupHeader = [NSString stringWithFormat:@"Header:%d",arc4random_uniform(1000)];
numberGroup.groupFooter = [NSString stringWithFormat:@"Footer:%d",arc4random_uniform(1000)];
numberGroup.groupIndex = [NSString stringWithFormat:@"%d",arc4random_uniform(10)];
NSMutableArray *tempArray = [NSMutableArray array];
for (int i = 0; i<10; i++) {
NSString *number = [NSString stringWithFormat:@"cell:%d",arc4random_uniform(100000)];
[tempArray addObject:number];
}
numberGroup.groupNumbers = tempArray;
return numberGroup;
}
@end
创建TableViewController
创建TableViewController.h 和TableViewController.m文件
在Main.storyboard中拖出一个TableViewController 点击title ->右侧第三个图标 Custom Class Class填写TableViewController
如果是第一个页面 第四个图标 View Controller ->Title is Initial View Controller 勾选
TableViewController.h
继承UITableViewController
#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController
@end
TableViewController.m
准备数据
定义一个属性为NSMutableArray的数组
@property(nonatomic,strong) NSMutableArray *dataList;
给数组赋值
-(NSMutableArray *)dataList {
if(_dataList == nil){
_dataList = [NSMutableArray array];
for (int i = 0; i < 4; i++) {
/* NSString *numberString = [NSString stringWithFormat:@"%d",
arc4random_uniform(1000)];
[_dataList addObject:numberString];
*/
NumberGroup *numberGroup = [NumberGroup numberGroup];
[_dataList addObject:numberGroup];
}
}
return _dataList;
}
可以给TableView设置背景颜色等属性 如果不使用Main.storyboard 界面的生成也在这个方法里写
-(void)viewDidLoad{
[super viewDidLoad];
self.tableView.sectionIndexColor = [UIColor redColor];
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
}
数据源方法实现
dataList.count:多少行数据
dataList[section]:多少段
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.dataList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return self.dataList.count;
//取出对应的section
NumberGroup *tempGroup = self.dataList[section];
return tempGroup.groupNumbers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
//定义一个静态的CELL标识符
static NSString *CellID = @"cell";
//不规范
//UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//优先从缓存池中查找ID为“cell”的单元格
//规范
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
//如果缓存池中没有 则创建
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellID];
}
//获取数据源
// NSString *numberString = self.dataList[indexPath.row];
//设置cell的内容
// cell.textLabel.text = numberString;
//cell.detailTextLabel.text = numberString;
//cell.imageView.image = [UIImage imageNamed:@"1"];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//分段方法 section:段 row:行
NumberGroup *tempGroup = self.dataList[indexPath.section];
NSString *numberString = tempGroup.groupNumbers[indexPath.row];
//设置cell的内容
cell.textLabel.text = numberString;
cell.detailTextLabel.text = numberString;
cell.imageView.image = [UIImage imageNamed:@"1"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//自带4种模式
/*
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton __TVOS_PROHIBITED, // info button w/ chevron. tracks
UITableViewCellAccessoryCheckmark, // checkmark. doesn't track
UITableViewCellAccessoryDetailButton
*/
// Configure the cell...
NSLog(@"cell:%@",cell);
return cell;
}
单元格的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *infoString = [NSString stringWithFormat:@"第%d段,第%d行",indexPath.section,indexPath.row];
UIAlertView *alartView = [[UIAlertView alloc]initWithTitle:@"提示" message:infoString delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alartView show];
}
单元格的编辑 如左划删除
//nullale表示对象可以是空或nil 而_nonnull表示对象不应该为空
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"刷新" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[tableView reloadData];
}];
UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self.dataList removeObjectAtIndex:indexPath.row];
//刷新表格
[tableView reloadData];
}];
NSArray *actionArray = @[action1,action2];
return actionArray;
}
设置header footer 和 索引
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NumberGroup *numberGroup = self.dataList[section];
return numberGroup.groupHeader;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
NumberGroup *numberGroup = self.dataList[section];
return numberGroup.groupFooter;
}
#pragma mark - 设置索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray *indexArray = [NSMutableArray array];
for (NumberGroup *numberGroup in self.dataList) {
[indexArray addObject:numberGroup.groupIndex];
}
return indexArray;
}
订制section的Header/Footer
-(nullable UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.image = [UIImage imageNamed:@"1"];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 100, 20)];
label.text = @"猜你喜欢";
label.font = [UIFont systemFontOfSize:14.0];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(250, 5, 60, 20);
[button setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickOpenButton) forControlEvents:UIControlEventTouchUpInside];
UIView *headerView = [[UIView alloc]init];
[headerView addSubview:imageView];
[headerView addSubview:label];
[headerView addSubview:button];
return headerView;
}
-(void) clickOpenButton{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"点击了header" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
}
参考:网易云课堂 详解UITableView