如果能准确知道数组容量的时候,不要用array直接实例化可变数组,[NSMutableArray arrayWihCapacity:50]; < 可优化不少。
。如果有需要跟踪NSArray中的内容,需要重写descriptionWithLocal 方法。首先新建一个NSArray 的Category,在新建出来的.m文件中加入下列代码
- (NSString *)descriptionWithLocal:(id)local
{
NSMutableString *string = [NSMutableString string];
[string appendString:@"("];
for (id obj in self) {
[string appendFormat:@" "%@",", obj];
}
[string appendString:@" )"];
return string;
}
如此一来,用NSLog显示NSArry中的数据不再是utf-8编码,显示为原来数据。
UITableView
需要一个数据源来显示数据
1.需加入数据源协议 UITableViewDataSource ; <
2. 设置表格的组数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView ; <
3.设置每个分组中的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section ; <
4.设置每个分组中显示的数据内容 - (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPaht:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIndentifier:nil];
indexPath.section 即为分组名 indexPath.row 即行名
} cell即是返回的分组中的数据。
[cell.textLabel setText: ]; <
[cell.imageView setImage: ]; <
[cell.detailTextLabel setText: ]; <
每个ViewCell中都有一个辅助指示视图 accessoryType 默认是UITableViewCellAccessoryNone 即不显示辅助指示视图 其他值如下
通过 cell setAccessoryType:UITableViewCellAccessoryNone ; < 来设置
5.设置分组的标题 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSIntger)secton ; <
6.设置页脚的标题, 分组底部的子标题 - (NSString *)tableView:(UITableVIew *)tableView titleForFooterInSection:(NSInteger)section ; <
7.字典返回所有keys的方法 NSArray *items = [self.cities allKeys]; < 里面数据出来的顺序是乱的
8.将UITableView中的分组分开(即设置style,但style只能在初始化的时候设定)
UITableView *tableView = [[UITableView alloc] initWithFrame: style:(UITableViewStyle)] ; <
9.代理方法: 选中某一行所进行的操作 - (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndex *)indexPaht ; <
右侧索引方法 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView ; <
设置行高 - (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath ; <
10.刷新某一行的数据 [tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationLeft]; <
11.刷新全部数据 [tableView reloadData];
12.更新某一行数据 [_tableView reloadRowsAtIndexPaths:[_tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationLeft];
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UITableView的优化:(条件为在storyBoard中的tableView中添加一个tableViewCell, 并设置identifier为myCell)不然会报错!
static NSString *cellIdentifier = @"myCell"; //设置统一的标示符 static 可以保证表格标识符永远只有一个
UITableViewCell *cell = [tableView dequeueReusableCellWithIndentifier:cellIndentifier]; //首先在缓存池中找名为''myCell''的单元格对象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)#> forIndexPath:<#(NSIndexPath *)#>] //自定义单元格时使用
if (cell = nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellstyleDefault reuseIdentifier:cellIdentifier]; //如果没找到,实例化一个新的cell
} ios中,当页面内的tableView能容纳10行,当向下滑动时,第一行的数据地址进入缓存池,新刷出来的第十一行将从缓存池取出原第一行的地址用于存储第十一行数据
如果使用了storyBoard,并且,在storyBoard中指定了"单元格"的可重用标识符,那么
dequeueReusableCellWithIdentifier
dequeueReusableCellWithIdentifier
第一种情况:
如果在viewDidLoad注册了nib文件 / 如果在viewDidLoad注册了自定义单元格的类,并且指定了''单元格''的可重用标识符,那么
dequeueReusableCellWithIdentifier
dequeueReusableCellWithIdentifier:forIndexPath 两个方法等效
第二种情况:
如果没有在viewDidLoad注册nib文件 / 没有在viewDidLoad注册自定义单元格的类,那么,只能使用
dequeueReusableCellWithIdentifier 并且需要判断cell没有被实例化,并做相应的处理
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
通过拖动控件的方式来创建TableViewControl时,需注意设置tableViewCell的identifier!
用segue进行View的切换 在prepareForSegue方法中可找到所有的segue,但不同的segue的标识符不一样
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
CitiesViewController *controller = segue.destinationViewController ;
[controller setCities:cities]; 在CitiesViewController.h文件中定义了cities这一属性,并用这一属性构造了视图,此处只需用cities设置属性即可
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
[tableView registerClass:[chatCell class] forCellReuseIdentifier:@"myCell"] 为表格注册自定义单元格的类,并指定单元格的可重用"标识符"
----------------
1.在storyBoard中直接自定义单元格会注册单元格原型
2.用xib方式自定义单元格需要注册NIB
UINib *nib = [UINib nibWithNibName:@"bookCell" bundle:[NSBundle mainBundle]];
[tableView registerNib:nib forCellReuseIdentifier:@"cell"];
3.用代码方式自定义单元格需要注册类
[tableView registerClass:[MyCell class] forCellReuseIdentifier:CellIdentifier];
以下方法的目的就是要求必须注册自定义单元格类
[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
细节决定成败!