打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
UISearchController / UIAlertController / UITableViewRowAction
    在iOS8中,发生了一些变化,这里介绍下UISearchController、UIAlertController、UITableViewRowAction这些的信息,写了一个简易的Demo
有不正确的地方欢迎指出












// 主要代码:
// UISearchController

//创建出搜索使用的表示图控制器

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];






// UIAlertController

#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];

 

}




下载地址:Demo下载
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
tableView:editActionsForRowAtIndexPath:  UITableViewRowAction
UITableView 实现Cell删除操作
UITableView侧滑删除
点击UITableView的cell展开收缩
iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UIS
UITableView多选删除,类似mail中的多选删除效果
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服