前言
工作项目中有这样一个需求:目标控制器使用TableView展示一组网络请求返回的数组,由上一个控制器push过来时传入一个或多个该数组的元素,需要在展示时优先展示并默认选中传进来的数据。
需求分析
功能实现
首先将接收到的元素截取并存入数组,并打开TableView编辑模式1
2
3
4//进入编辑模式
[self.tableView setEditing:YES animated:YES];
//接收ID,截取为数组
draftSendID = [NSMutableArray arrayWithArray:[self.receiverIds componentsSeparatedByString:@","]];//数组
发送网络请求,将返回的JSON模型化并存入数组,根据上一个上一个控制器传入的ID进行比对排序。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17[infoarray addObject:[ReceiversModel instanceWithDict:item]];
//排序
NSArray *array1 = [infoarray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
ReceiversModel *model1 = [[ReceiversModel alloc]init];
model1 = obj1;
ReceiversModel *model2 = [[ReceiversModel alloc]init];
model2 = obj2;
if (![draftSendID containsObject:model1.receiverId] ){
return NSOrderedDescending;
}else if([draftSendID containsObject:model1.receiverId]){
return NSOrderedAscending;
}else{
return NSOrderedSame;
}
}];
数据源中实现选中1
2
3
4
5
6
7
8
9
10
11
12
13
14
15- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
...
...
ReceiversModel *model = infoarray[indexPath.row];
if ([draftSendID containsObject:model.receiverId] ) {
[receiverIdsArray addObject:model];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
[cell setCellWithModel:model];
return cell;
}
以下三个TableView代理方法必须要实现1
2
3
4
5
6
7
8
9
10
11
12- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//选中编辑模式
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
}
选中实现方法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- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ReceiversModel *model = infoarray[indexPath.row];
if (![receiverIdsArray containsObject:model] ) {
[receiverIdsArray addObject:model];
}
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
//可通过下方方法实现单选,此处不适用
// NSInteger newRow = [indexPath row];
//
// NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
// if (newRow != oldRow)
//
// {
// [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
// UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
//
// oldCell.accessoryType = UITableViewCellAccessoryNone;
//
// [tableView deselectRowAtIndexPath:lastIndexPath animated:YES];
// lastIndexPath = [indexPath copy];
//
// }else{
//
// [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
//
// }
}
取消选中方法,进行判断1
2
3
4
5
6- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
ReceiversModel *model = infoarray[indexPath.row];
if ([receiverIdsArray containsObject:model] ) {
[receiverIdsArray removeObject:model];
}
}
最后再拼接字符串通过代理回传到上一个控制器就o了