標籤:uitableviewcell 左劃 編輯 左劃刪除
今天來介紹下iOS開發中UITableView的Cell左劃實現中置頂,刪除等功能。該功能在iOS8.0以前是需要很複雜的實現,不過github上應該有現成demo,不過今天介紹的是在iOS8.0以後蘋果新推出的api,來實現Cell左劃自訂控制項。
1. 首先建立UITableView視圖,實現其倆個代理,UITableViewDelegate和UITableViewDataSource,該處代碼就不說了,主要是倆個回調方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
和-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2. 然後實現另一個代理方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
editingStyle = UITableViewCellEditingStyleDelete;//此處的EditingStyle可等於任意UITableViewCellEditingStyle,該行代碼只在iOS8.0以前版本有作用,也可以不實現。
}
3. 再實現
`-(NSArray )tableView:(UITableView )tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@”刪除” handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定義
NSLog(@”點擊刪除”);
}];//此處是iOS8.0以後蘋果最新推出的api,UITableViewRowAction,Style是划出的標籤顏色等狀態的定義,這裡也可自行定義
UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"編輯" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {}];editRowAction.backgroundColor = [UIColor colorWithRed:0 green:124/255.0 blue:223/255.0 alpha:1];//可以定義RowAction的顏色 return @[deleteRoWAction, editRowAction];//最後返回這倆個RowAction 的數組
}`
這樣就實現了如下的效果
另附:
iOS8.0以前實現UITableViewCell左劃多個標籤的Demo參考
iOS項目開發小技巧 (三) --UITableView實現Cell左劃刪除等自訂功能