iOS項目開發小技巧 (三) --UITableView實現Cell左劃刪除等自訂功能

來源:互聯網
上載者:User

標籤: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左劃刪除等自訂功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.