《精通 iOS 开发》第 08 章学习笔记

实现简单表

1.控制器头文件声明遵循的协议:

1
2
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end

2.视图加载完毕后调整表的顶部位置:

1
2
3
4
UITableView *tableView = (id)[self.view viewWithTag:1];
UIEdgeInsets contentInsert = tableView.contentInset;
contentInsert.top = 20;
[tableView setContentInset:contentInsert];

3.设置表的总行数:

1
2
3
4
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.dwarves count];
}

4.设置表的各行数据(UITableViewCellStyleDefault参数修改表的样式):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"SimpleTableIdentifier";

UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}

UIImage *image = [UIImage imageNamed:@"star"];
cell.imageView.image = image;

cell.textLabel.text = self.dwarves[indexPath.row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];

if (indexPath.row < 7) {
cell.detailTextLabel.text = @"Mr. Disney";
} else {
cell.detailTextLabel.text = @"Mr. Tolkien";
}

return cell;
}

5.设置表的每行缩进:

1
2
3
4
- (NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath.row;
}

6.设置表的某一行不能被选择:

1
2
3
4
5
6
7
8
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return nil;
} else {
return indexPath;
}
}

7.设置表的每行高度:

1
2
3
4
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}

8.当表的某一行被选中后的操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *rowValue = self.dwarves[indexPath.row];
NSString *message =
[[NSString alloc] initWithFormat:@"You selected %@", rowValue];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:@"Yes, I Did"
otherButtonTitles:nil];

[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

代码实现定制表视图单元

1.单元格类代码布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CGRect nameLabelRect = CGRectMake(0, 5, 70, 15);
UILabel *nameMarker = [[UILabel alloc] initWithFrame:nameLabelRect];

nameMarker.textAlignment = NSTextAlignmentRight;
nameMarker.text = @"Name:";
nameMarker.font = [UIFont boldSystemFontOfSize:12];
[self.contentView addSubview:nameMarker];

CGRect colorLabelRect = CGRectMake(0, 26, 70, 15);
UILabel *colorMarker = [[UILabel alloc] initWithFrame:colorLabelRect];

colorMarker.textAlignment = NSTextAlignmentRight;
colorMarker.text = @"Color:";
colorMarker.font = [UIFont boldSystemFontOfSize:12];
[self.contentView addSubview:colorMarker];

CGRect nameValueRect = CGRectMake(80, 5, 200, 15);
_nameLabel = [[UILabel alloc] initWithFrame:nameValueRect];
[self.contentView addSubview:_nameLabel];

CGRect colorValueRect = CGRectMake(80, 25, 200, 15);
_colorLabel = [[UILabel alloc] initWithFrame:colorValueRect];
[self.contentView addSubview:_colorLabel];
}
return self;
}

2.定义全局复用的单元格标识:

1
static NSString *CellTableIdentifier = @"CellTableIdentifier";

3.视图控制器加载完毕后,注册表单元类:

1
2
3
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[NameAndColorTableViewCell class]
forCellReuseIdentifier:CellTableIdentifier];

4.设置表的总行数:

1
2
3
4
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.computers count];
}

5.设置表的各行数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NameAndColorTableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier
forIndexPath:indexPath];

NSDictionary *rowData = self.computers[indexPath.row];

cell.name = rowData[@"Name"];
cell.color = rowData[@"Color"];

return cell;
}

Nib文件加载实现定制表视图单元

1.视图控制器加载完毕后,从Nib文件中注册表单元类:

1
2
3
4
5
UITableView *tableView = (id)[self.view viewWithTag:1];
tableView.rowHeight = 65;

UINib *nib = [UINib nibWithNibName:@"BIDNameAndColorCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifier];

实现分组分区

1.视图加载完成之后,加载数据:

1
2
3
4
5
6
7
8
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:SectionTableIdentifier];

NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];

self.names = [NSDictionary dictionaryWithContentsOfFile:path];
self.keys = [[self.names allKeys] sortedArrayUsingSelector:@selector(compare:)];

2.设置每个分区的数据个数:

1
2
3
4
5
6
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
NSString *key = self.keys[section];
NSArray *nameSection = self.names[key];
return [nameSection count];
}

3.设置每个分区的标题:

1
2
3
4
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
return self.keys[section];
}

实现索引分区

1.设置索引标题:

1
2
3
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.keys;
}

2.处理顶端边缘偏移的问题:

1
2
3
4
5
if (tableView.style == UITableViewStylePlain) {
UIEdgeInsets contentInset = tableView.contentInset;
contentInset.top = 20;
[tableView setContentInset:contentInset];
}

3.处理状态栏被干扰的问题:

1
2
3
4
UIView *barBackground =
[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
barBackground.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
[self.view addSubview:barBackground];

实现搜索栏

1.控制器头文件声明遵循的协议:

1
2
3
@interface SectionsViewController
: UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate>
@end

2.添加实例变量保存搜索匹配的名字:

1
2
NSMutableArray *filteredNames;
UISearchDisplayController *searchController;

3.搜索栏初始化操作:

1
2
3
4
5
6
7
filteredNames = [NSMutableArray array];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

tableView.tableHeaderView = searchBar;
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;

4.加载搜索结果:

1
2
3
4
5
- (void)searchDisplayController:(UISearchDisplayController *)controller
didLoadSearchResultsTableView:(UITableView *)tableView {
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:SectionTableIdentifier];
}

5.需重新加载搜索结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString {
[filteredNames removeAllObjects];
if (searchString.length > 0) {
NSPredicate *predicate =
[NSPredicate predicateWithBlock:^BOOL(NSString *name, NSDictionary *b) {
NSRange range =
[name rangeOfString:searchString options:NSCaseInsensitiveSearch];
return range.location != NSNotFound;
}];
for (NSString *key in self.keys) {
NSArray *matches =
[self.names[key] filteredArrayUsingPredicate:predicate];
[filteredNames addObjectsFromArray:matches];
}
}
return YES;
}

6.设置分区索引的颜色:

1
2
3
tableView.sectionIndexBackgroundColor = [UIColor blackColor];
tableView.sectionIndexTrackingBackgroundColor = [UIColor darkGrayColor];
tableView.sectionIndexColor = [UIColor whiteColor];