在iOS8中,发生了一些变化,这里介绍下UISearchController、UIAlertController、UITableViewRowAction这些的信息,写了一个简易的Demo
//创建出搜索使用的表示图控制器
self.searchTVC=[[UITableViewControlleralloc]initWithStyle:UITableViewStylePlain];
_searchTVC.tableView.dataSource=self;
_searchTVC.tableView.delegate=self;
//使用表示图控制器创建出搜索控制器
self.searchController=[[UISearchControlleralloc]initWithSearchResultsController:_searchTVC];
//搜索框检测代理
//(这个需要遵守的协议是,这个协议中只有一个方法,当搜索框中的值发生变化的时候,代理方法就会被调用)
_searchController.searchResultsUpdater=self;
_searchController.searchBar.placeholder=@"姓名|电话 |性别";
_searchController.searchBar.prompt=@"输入要查找的混蛋";
//因为搜索是控制器,所以要使用模态推出(必须是模态,不可是push)
[selfpresentViewController:_searchControlleranimated:YEScompletion:nil];
#pragma mark 处理cell的点击事件
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
// 获取到点击的模型
Student*stu = (tableView == self.tableView? _allDataArray[indexPath.row]: _searchResultDataArray[indexPath.row]);
// 弹出AlertView显示
UIAlertController*alertController= [UIAlertControlleralertControllerWithTitle:@"谁点了这个混蛋!"message:[NSStringstringWithFormat:@"姓名:%@\n电话:%@\n性别:%@",stu.name,stu.phoneNumber,stu.gender]preferredStyle:UIAlertControllerStyleAlert];
// 添加关闭按钮
UIAlertAction*cancelAction= [UIAlertActionactionWithTitle:@"关闭"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*action){
}];
[alertController addAction:cancelAction];
// 在搜索结果的tableView中,需要使用_searchTVC来推出提示框,否则使用self即可
UIViewController*currentVC = tableView == self.tableView? self: _searchTVC;
// 推出提示框
[currentVCpresentViewController:alertControlleranimated:YEScompletion:nil];
}
// UITableViewRowAction
#pragma mark 设置滑动显示出的按钮
- (NSArray*)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath
{
// 置顶按钮
UITableViewRowAction *layTopRowAction= [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"置顶"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath){
NSLog(@"点击了置顶");
[tableView setEditing:NOanimated:YES];
}];
layTopRowAction.backgroundColor= [UIColorredColor];
// 标记
UITableViewRowAction *markRowAction= [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"标记混蛋"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath){
NSLog(@"标记了这个混蛋");
[tableView setEditing:NOanimated:YES];
}];
markRowAction.backgroundColor= [UIColorgreenColor];
// 更多按钮
UITableViewRowAction *moreRowAction= [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"更多"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath){
NSLog(@"点击更多");
[tableView setEditing:NOanimated:YES];
}];
moreRowAction.backgroundColor= [UIColordarkGrayColor];
// 返回编辑按钮
return@[layTopRowAction,markRowAction, moreRowAction];
}