實(shí)現(xiàn)效果
實(shí)現(xiàn)系統(tǒng)原生的刪除方法
- 必須要實(shí)現(xiàn)系統(tǒng)原生的兩個(gè)方法
- 方法
editingStyleForRowAtIndexPath:
為設(shè)置表格的樣式為刪除樣式; - 方法
titleForDeleteConfirmationButtonForRowAtIndexPath:
為編輯時(shí)顯示的文字,這里講的是自定義,所以這里的文字可以傳空字符串,避免自定義后文字重疊異常顯示, 系統(tǒng)默認(rèn)顯示的文字為 "刪除"枕稀;
- 方法
注意: 這里的傳的空字符串的長度決定你自定義后顯示多少個(gè)按鈕的總寬度策精。
#pragma mark - <UITableViewDelegate>
/**
* 實(shí)現(xiàn)表格系統(tǒng)方法的編輯模式為刪除模式
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
/**
* 設(shè)置滑出cell后的文字為空字符串
*/
- (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
return @" ";
}
數(shù)據(jù)源的代理方法實(shí)現(xiàn)不需要額外設(shè)置什么
#pragma mark - <UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifie = @"UITableViewCell";
GJEmployeeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifie];
if (!cell) {
cell = [[GJEmployeeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifie];
}
//選擇的cell
WEAKSELF
cell.slectedActionBlock = ^(SwipeCellActionType cellActionType, UITableViewCell *cell){
//防止cell出現(xiàn)選中效果
NSIndexPath *editIndex = [tableView indexPathForCell:cell];
[tableView reloadRowsAtIndexPaths:@[editIndex] withRowAnimation:UITableViewRowAnimationNone];
GJEmployeeModel *editEmployee = weakSelf.dataArr[editIndex.row];
if (cellActionType == EditCellType) {
//處理編輯按鈕事件...
} else {
//處理刪除按鈕事件...
}
};
return cell;
}
自定義Cell(.m)文件的邏輯
- 關(guān)鍵點(diǎn):重寫cell的
layoutSubviews
方法 - 在
layoutSubviews
方法中遍歷尋找到滑動(dòng)完Cell后底部顯示的視圖, 然后再添加需要自定義的按鈕的到底部的視圖上;
注意: 這里要根據(jù)當(dāng)前的手機(jī)系統(tǒng)版本來區(qū)分底部的視圖是屬于哪種類型的視圖:
1.在iOS8以下的系統(tǒng)經(jīng)過親自測試遍歷到底部的視圖屬于:UITableViewCellDeleteConfirmationView
這種類型的視圖;
2.在iOS8以上的系統(tǒng)經(jīng)過親自測試遍歷到底部的視圖屬于:_UITableViewCellActionButton
這種類型的視圖;
/**
* 根據(jù)ios系統(tǒng)版本自定義滑動(dòng)刪除按鈕
*/
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
//根據(jù)ios系統(tǒng)版本判斷滑動(dòng)刪除視圖View, (關(guān)鍵部分)
NSString *systemDeleteViewName = nil;
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
systemDeleteViewName = @"UITableViewCellDeleteConfirmationView";
} else {
systemDeleteViewName = @"_UITableViewCellActionButton";
}
if ([NSStringFromClass([subview2 class]) isEqualToString:systemDeleteViewName]) {
NSLog(@"subview2 屬于什么View====%@",subview2);
//防止多次添加自定義按鈕
[subview2.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//subview2的寬度由控制器里面的代理方法(tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:)返回的文字寬度決定
[subview2 addSubview:self.editButton];
self.editButton.width = subview2.width/2;
[subview2 addSubview:self.deleteButton];
self.deleteButton.x = CGRectGetMaxX(self.editButton.frame);
self.deleteButton.width = self.editButton.width;
NSLog(@"subview2====%zd",subview2.subviews.count);
}
}
}
}
- (UIButton *)editButton
{
if (!_editButton) {
_editButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, self.height)];
_editButton.backgroundColor = UIColorFromRGB(0xbbbbbb);
[_editButton setImage:ImageNamed(@"editButton_icon") forState:0];
_editButton.tag = EditCellType;
[_editButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _editButton;
}
- (UIButton *)deleteButton
{
if (!_deleteButton) {
_deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(80, 0, 80, self.height)];
_deleteButton.backgroundColor = UIColorFromRGB(0xfe7270);
[_deleteButton setImage:ImageNamed(@"deleteButton_icon") forState:0];
_deleteButton.tag = DeleteCellType;
[_deleteButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _deleteButton;
}
- (void)buttonAction:(UIButton *)button
{
if (self.slectedActionBlock) {
self.slectedActionBlock(button.tag, self);
}
}
總結(jié)
以上實(shí)現(xiàn)自定義的方法的關(guān)鍵點(diǎn)在于遍歷到Cell底部的視圖, 這種遍歷的方法可能會(huì)隨著iOS手機(jī)系統(tǒng)的版本不同而Cell層次結(jié)構(gòu)也不同, 因此在最新的iOS10.2以后的版本中可能存在遍歷不到的情況, 經(jīng)過親自測試,目前最新的iOS10.2以下的版本可用上面的方法親測有效;