欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中tableview實(shí)現(xiàn)編輯、全選及刪除等功能的方法示例

 更新時(shí)間:2017年07月02日 16:35:44   作者:去你的聯(lián)盟  
這篇文章主要給大家介紹了關(guān)于iOS中tableview實(shí)現(xiàn)編輯、全選及刪除等功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),不僅是介紹實(shí)現(xiàn)的方法,將實(shí)現(xiàn)過程中遇到的問題也都分享出來了,需要的朋友們下面來一起看看吧。

前言

我們在日常開發(fā)過程中或多或少都會(huì)遇到tableview的各種功能,這里簡單記錄一下tableview的刪除和全選刪除功能,廢話不多說先看一下效果圖


既然拿到了需求,就應(yīng)該想一下如何去實(shí)現(xiàn)了,對照上面圖片的內(nèi)容,應(yīng)該如何實(shí)現(xiàn)呢?

看完上圖之后發(fā)現(xiàn)用到的幾個(gè)功能:

第一個(gè):左滑刪除

第二個(gè):全選刪除

左邊滑動(dòng)刪除

實(shí)現(xiàn)幾個(gè)代理方法后就可以了

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
 return @"刪除";
}

改變左滑后按鈕的文字

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  return UITableViewCellEditingStyleDelete; 
}

滑動(dòng)刪除樣式,有多中可選,這里返回刪除樣式

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
  [self.dataArray removeObjectAtIndex: indexPath.row];
  [self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]
        withRowAnimation:UITableViewRowAnimationFade];
  [self.tableView reloadData];

 }
}

刪除點(diǎn)擊方法,處理想要?jiǎng)h除的數(shù)據(jù)

這里有一個(gè)需要注意點(diǎn),一定要先更新數(shù)據(jù)源,在更新UI

左滑刪除就這些代碼了,是不是很easy,在來看全選的代碼

全選刪除

這里我用的是全選功能是系統(tǒng)的方法,沒有自定義按鈕

點(diǎn)擊編輯按鈕的時(shí)候設(shè)置tableview

  [_tableView setEditing:YES animated:YES];

返回全選的樣式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}

這樣就會(huì)出現(xiàn)左側(cè)的選中框

再來就是全選按鈕的實(shí)現(xiàn)方法

 for (int i = 0; i< self.dataArray.count; i++) {
   NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
   [_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
  }

  if (self.deleteArray.count >0) {
   [self.deleteArray removeAllObjects];
  }
  [self.deleteArray addObjectsFromArray:self.dataArray];

  [btn setTitle:@"取消" forState:UIControlStateNormal];

當(dāng)然取消全選也有方法

 for (int i = 0; i< self.dataArray.count; i++) {
   NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
   [_tableView deselectRowAtIndexPath:indexPath animated:NO];

  }

通過全選按鈕實(shí)現(xiàn)的選中方法,需要在方法里把所有數(shù)據(jù)都添加到想要?jiǎng)h除的數(shù)組里面

通過點(diǎn)擊tableviewcell選擇刪除對象的時(shí)候需要把想要?jiǎng)h除的數(shù)據(jù)添加到刪除數(shù)組里面

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 if (self.btn.selected) {
   NSLog(@"選中");
  [self.deleteArray addObject:[self.dataArray objectAtIndex:indexPath.row]];

 }else{
  NSLog(@"跳轉(zhuǎn)下一頁");
 }
}

再次點(diǎn)擊取消選中的數(shù)據(jù)

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

 if (self.btn.selected) {
  NSLog(@"撤銷");
  [self.deleteArray removeObject:[self.dataArray objectAtIndex:indexPath.row]];

 }else{
  NSLog(@"取消跳轉(zhuǎn)");
 }

}

問題一:

按照以上方法實(shí)現(xiàn)之后就可以實(shí)現(xiàn)想要的功能,但是還有UI的問題,那就是選擇之后會(huì)出現(xiàn)下圖的問題


會(huì)有一層背景色的覆蓋,想要了解的請看看這篇文章 http://www.dbjr.com.cn/article/117619.htm

問題二:

還有一個(gè)問題 ,在自定義的cell上面添加控件的時(shí)候一定要添加到self.contentView上面,否則會(huì)出現(xiàn)控件不隨cell移動(dòng)的問題

 [self.contentView addSubview:self.label];

結(jié)束

到這里這篇文章的內(nèi)容基本算完結(jié)了,如果還是有不明白的我在此留下Demo鏈接,里面有更詳細(xì)的注釋,Demo沒有做UI適配,想看效果的畫在模擬器6,7上面運(yùn)行最好

Demo地址:http://git.oschina.net/T1_mine/tableviewedit

本地下載地址:http://xiazai.jb51.net/201707/yuanma/tableviewedit(jb51.net).rar

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評(píng)論