27.iOS中最重要的视图UITableView表视图第二章

本文详细介绍了如何使用Swift和UIKit构建一个iOS应用,实现数据的高效展示和用户交互。通过创建自定义视图控制器、数据模型类、以及表格视图组件,文章深入探讨了数据获取、解析、存储及展示的全过程。重点在于如何优化数据处理流程,提升用户体验,同时确保代码的可维护性和可扩展性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

准备1:添加了一个数据源Class25ContactList.plist

================================

准备2:创建一个CellModel类

字典中有几个键值对 就要创建几个属性

注意:属性的名字 就是键值对中的key 要完全一致

@interface CellModel : NSObject
@property (nonatomic,retain)NSString *name;
@property (nonatomic,retain)NSString *gender;
@property (nonatomic,retain)NSString *age;
@property (nonatomic,retain)NSString *phoneNumber;
@property (nonatomic,retain)NSString *hobby;
@property (nonatomic,retain)NSString *picture;
@end

在CellModel.m”中

kvc间接访问属性 保护方法 必须要实现的

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{

可以捕获错误的key 可以打印出来

NSLog(@”%@”,key);
}

================================

准备3:创建一个视图控制器(接收传值的)

创建一个继承于UIViewController的控制器

@interface SecondViewController : UIViewController

声明属性用于接收数据

@property (nonatomic,retain)NSString *name;
@property (nonatomic,retain)NSString *gender;
@property (nonatomic,retain)NSString *age;
@property (nonatomic,retain)NSString *phoneNumber;
@property (nonatomic,retain)NSString *hobby;
@property (nonatomic,retain)NSString *picture;
@end

===================================

创建表视图

-(void)addTableView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
[tableView release];
}

添加数据处理的方法

- (void)setUpData
{

取出路径

NSString *path = [[NSBundle mainBundle]pathForResource:@”Class25ContactList” ofType:@”plist”];

用字典接收路径(路径里是一个字典)

NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];

创建一个字典保存新的字典

self.dataDic = [NSMutableDictionary dictionary];

取出所有key

NSArray *keys = dic.allKeys;
for (int i = 0; i < keys.count; i++) {

取出每一个key

NSString *key = keys[i];

创建一个临时数组

NSMutableArray *values = [NSMutableArray array];

取出每一个数组

NSArray *temparray = dic[key];

遍历每一个temparray

for (NSDictionary *oneDic in temparray) {

创建model

CellModel *model = [[CellModel alloc] init];

给model赋值

[model setValuesForKeysWithDictionary:oneDic];

你给我一个字典 我还你一个赋值完成model

[values addObject:model];

释放

[model release];
}

重新构建 字典的键值对

[self.dataDic setObject:values forKey:key];
}

取出所有的key

NSArray *akeys = self.dataDic.allKeys;

对key进行排序

self.sortKeysArray = [akeys sortedArrayUsingSelector:@selector(compare:)];

}

设置有多少个分区

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataDic.count;
}

设置每一区有多少行(必须实现)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

用分区 取出对应的key

NSString *key = self.sortKeysArray[section];

用这个key取出对应的数组

NSArray *values = self.dataDic[key];
return values.count;
}

必须实现

- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @”Mycell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
}

取出对应的key

NSString *key = self.sortKeysArray[indexPath.section];

取出key对应的value

NSArray *values = self.dataDic[key];

用model接收数组中的model

CellModel *model = values[indexPath.row];
cell.textLabel.text = model.name;
cell.detailTextLabel.text = model.phoneNumber;
return cell;
}

设置分区标头的标题

- (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
{
return self.sortKeysArray[section];
}

设置右边的按钮

- (NSArray )sectionIndexTitlesForTableView:(UITableView )tableView
{
return self.sortKeysArray;
}

实现点击跳转传值

- (void)tableView:(UITableView )tableView didUnhighlightRowAtIndexPath:(NSIndexPath )indexPath
{
SecondViewController *secondVC = [[SecondViewController alloc] init];

取出对应分区的key

NSString *key = self.sortKeysArray[indexPath.section];

取出对应的values

NSArray *values = self.dataDic[key];

取出对应的model

CellModel *model = values[indexPath.row];

传值

secondVC.name = model.name;
secondVC.age = model.age;
secondVC.gender = model.gender;
secondVC.phoneNumber = model.phoneNumber;
secondVC.picture = model.picture;
secondVC.hobby = model.hobby;

调转页面

[self.navigationController pushViewController:secondVC animated:NO];
}

===================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值