1.UIImageView图片控件(类比于Android中的ImageView控件):
UIImageView是用来在屏幕上显示图片的一个视图,如要显示图片,首先需要将图片文件加载到UIImage上,然后通过方法使用UIImage。
- imageNamed:通过项目中的文件来创建;
- imageWithCGImage:通过Quartz 2D对象来创建;
- imagewithContentsOfFile:通过指定路径创建;
- imageWithData:通过NSData创建。
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 150, 150)];
[imageView1 setImage:[UIImage imageNamed:@"hearted.jpg"]]; //第一种设置图片的方法
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"hearted.jpg" ofType:@"jpg"]; //第二种设置方式
UIImage *image1 = [UIImage imageWithContentsOfFile:filePath];
[imageView1 setImage:image1];
NSString *filePath1 = [[NSBundle mainBundle]pathForResource:@"hearted.jpg" ofType:@"jpg"]; //第d三种设置方式
NSData *data = [NSData dataWithContentsOfFile:filePath1];
UIImage *image2 = [UIImage imageWithData:data];
[imageView1 setImage:image2];
[self.view addSubview:imageView1];
[imageView1 release];
第一种方式系统会把图像文件缓存在内存当中,当图像文件大或者多时会消耗大量内存。当使用imageWithData方法时,系统会将图像文件以数据的形式加载到应用程序。
2.UITableView表视图控件(类比于Android中的ListView或RecyclerView):
UITableView一般用于显示文本内容或内容的编辑,其中有两种样式UITableViewStylePlain和UITableViewStyleGrouped。
每个表视图都由3个部分组成:tableHeaderView头视图/tableView正文部分/tableFooterView尾视图,头视图和尾视图是用来显示一些辅助的信息。
在表视图中每一行用于显示内容的部分称为单元格cell,每个cell也是由3个部分组成:头视图CellHeaderView/内容部分/尾视图CellFooterView。UITableViewStyleGrouped由多个section组成,每个section又由多个单元格cell组成。
2.1 UITableView的创建:
- 首先用到两个协议UITableViewDataSource和UITableViewDetegate:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *listofFile;
}
@end
- 实现协议中UITableViewDataSource设置数据数量的方法(类比于Android中RecyclerView里面adapter中getItemCount方法设置数据的数量):
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listofFile count];
}
- 实现协议中UITableViewDetegate设置数据分配的方法(类比Android中RecyclerView里面adapter中onBindViewHolder,通过holder实现重用来减少对象的创建),UITableView对象会将窗口外的UITableViewCell对象放入UITableViewCell的对象池中,用于单元格的重用。当UITableView对象要求返回一个UITableViewCell对象时,数据源DataSource就会查看对象池,如果没有就创建对象,有就重用。每个UITableViewCell对象都有一个reuseIdentifier属性,类型为NSString,通过表格视图传入特定的NSString对象,定义一个特定NSString类型的数据Identifier数据源就可以查询并获取一个可重用的UITableViewCell对象,通过对reuseIdentifier来判定两者是否类型一致:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentfier = @"nationality";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentfier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentfier]
autorelease];
}
NSString *cellView = [listofFile objectAtIndex:indexPath.row];
cell.textLabel.text = cellView;
return cell;
}
- 创建实例:
UITableView *table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStylePlain];
[table setDataSource:self];
listofFile = [[NSMutableArray alloc]init];
[listofFile addObject:@"美国"];
[listofFile addObject:@"中国"];
[listofFile addObject:@"法国"];
[listofFile addObject:@"英国"];
[self.view addSubview:table];
2.2 Group类型表视图的创建:
- 创建实例,这里可以实现一个空的视图。数据在协议的实现方法中去添加:
UITableView *table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
[table setDataSource:self];
[table setDelegate:self];
self.view = table;
[table release];
- 在代理实现方法中设置section的个数(类比于Android中RecyclerView里面adapter中getItemViewType方法作用):
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- 在代理实现方法中设置section的行数row:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 1 || section == 2) {
return 2;
}
return 1;
}
- 向UITableView添加数据,indexPath属性用于标识当前cell在表视图中的位置:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int section = indexPath.section;
int row = indexPath.row;
static NSString *cellIdentifier = @"nationality";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
switch (section) {
case 0:
cell.textLabel.text = @"美国";
break;
case 1:
if (row == 0) {
cell.textLabel.text = @"英国";
} else {
cell.textLabel.text = @"德国";
}
break;
case 2:
if (row == 0) {
cell.textLabel.text = @"中国";
} else {
cell.textLabel.text = @"日本";
}
break;
default:
break;
}
}
return cell;
}
- 添加HeaderView视图:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return @"美洲";
break;
case 1:
return @"欧洲";
break;
case 2:
return @"亚洲";
break;
default:
return @"";
break;
}
}
2.3 UITableView相关属性:
可以在每个cell设置相关的字体颜色/大小/分隔线等属性。
cell.textLabel.textColor = [UIColor redColor];
2.4 按钮的点击事件:
通过实现代理方法,获取indexPath参数来添加要响应的事件。
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
//具体事件的处理
}
2.5 表视图的编辑模式:
- 删除单元格记录:删除一行数据第一步要从数组中移除相应的数据内容;第二步在表视图中删除相应的数据内容类似于RecyclerView适配器中删除数组,之后notifyItemRemove(postion),notifyDataSetChanged()进行视图刷新。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[listofFile removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- 添加单元格记录:同删除一样数组中要添加数据,表视图中也要添加数据(类似于RecyclerView适配器中删除数组,之后notifyItemInsert(postion),notifyDataSetChanged()进行视图刷新)。
NSIndexPath *index = [NSIndexPath indexPathWithIndex:indexPath.row + 1];
[tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
删除和添加单元格功能实现原理:会向DataSource发送tableView:canEditAtIndexPath:消息询问每个indexPath是否可以编辑,返回YES则会向代理delegate发送tableView:editingStyleForRowAtIndexPath:消息,询问编辑模式时插入还是删除。最后在commiEditingStyle:中执行操作。
- 重新排列单元格记录:
同样也是第一步数组中移除需要移动单元格在表视图中的记录,第二步在数组中需要移动到位置上添加对应的记录。