開(kāi)發(fā)過(guò)程中或多或少都會(huì)遇到tableview的各種功能丹拯,這里簡(jiǎn)單記錄一下tableview的刪除和全選刪除功能站超,廢話不多說(shuō)先看一下效果圖
既然拿到了需求,就應(yīng)該想一下如何去實(shí)現(xiàn)了乖酬,對(duì)照上面圖片的內(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,在來(lái)看全選的代碼
全選刪除
這里我用的是全選功能是系統(tǒng)的方法素挽,沒(méi)有自定義按鈕
點(diǎn)擊編輯按鈕的時(shí)候設(shè)置tableview
[_tableView setEditing:YES animated:YES];
返回全選的樣式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}
這樣就會(huì)出現(xiàn)左側(cè)的選中框
再來(lái)就是全選按鈕的實(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];
}
通過(guò)全選按鈕實(shí)現(xiàn)的選中方法蔑赘,需要在方法里把所有數(shù)據(jù)都添加到想要?jiǎng)h除的數(shù)組里面
通過(guò)點(diǎn)擊tableviewcell選擇刪除對(duì)象的時(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)下一頁(yè)");
}
}
再次點(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)");
}
}
問(wèn)題一:
按照以上方法實(shí)現(xiàn)之后就可以實(shí)現(xiàn)想要的功能,但是還有UI的問(wèn)題预明,那就是選擇之后會(huì)出現(xiàn)下圖的問(wèn)題
會(huì)有一層背景色的覆蓋缩赛,這里感謝一位簡(jiǎn)友的方法,想要了解的請(qǐng)看看這篇文章
http://www.reibang.com/p/af08a40a8821
問(wèn)題二:
還有一個(gè)問(wèn)題 ,在自定義的cell上面添加控件的時(shí)候一定要添加到self.contentView上面,否則會(huì)出現(xiàn)控件不隨cell移動(dòng)的問(wèn)題
[self.contentView addSubview:self.label];
結(jié)束
到這里這篇文章的內(nèi)容基本算完結(jié)了撰糠,如果還是有不明白的我在此留下Demo鏈接酥馍,里面有更詳細(xì)的注釋,Demo沒(méi)有做UI適配阅酪,想看效果的畫在模擬器6旨袒,7上面運(yùn)行最好