需求如圖

2.2.1 我的好友.jpg
拿到需求圖,我第一想法都是:系統(tǒng)的能不能搞?問(wèn)了幾個(gè)朋友,他們也遇到過(guò)類(lèi)似的需求,但他們都是自定義cell后添加手勢(shì)來(lái)做,我自己摸索了一下,發(fā)現(xiàn)系統(tǒng)自帶的可以完成這個(gè)需求,下面是具體的講解.
1.實(shí)現(xiàn)UITableView的兩個(gè)代理方法返回對(duì)應(yīng)的東西,便可以左滑出現(xiàn)多個(gè)action,
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;
2.一般情況下,僅僅是實(shí)現(xiàn)上述兩個(gè)方法,是不能改變UITableViewRowAction的背景顏色的,UITableViewRowAction的實(shí)例對(duì)象的背景顏色只能由UITableViewRowActionStyle決定,很遺憾沒(méi)有很多選擇,更沒(méi)有你想要的尊貴的橙色.
typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {
UITableViewRowActionStyleDefault = 0,//紅色
UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,//紅色
UITableViewRowActionStyleNormal //灰色
}
3.通過(guò)小面包,還是找到了一線希望.

截圖.png
圖紙箭頭指向的位置,如果是左滑狀態(tài),會(huì)有三個(gè)子控件.那個(gè)第三者就是: UITableViewCellDeleteConfirmationView.很明顯,左滑出現(xiàn)的按鈕就是添加到這個(gè)控件中去的.拿到了父控件,你就可以往里面添加你想要的控件了,想要什么款式就有什么款式.
下面貼上我自己實(shí)現(xiàn)需求的代碼
//類(lèi)中有一個(gè)leftTitleArr可變數(shù)組:UITableViewRowAction將要顯示的文字集合.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.tableView == tableView) {
NSString *title = self.leftTitleArr[indexPath.row];
UITableViewRowAction *actionLeft = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:title handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
if ([action.title isEqualToString:@"恢復(fù)關(guān)注"]) {
[self.leftTitleArr replaceObjectAtIndex:indexPath.row withObject: @"不再關(guān)注"];
action.backgroundColor = [UIColor lightGrayColor];
}else if ([action.title isEqualToString:@"不再關(guān)注"]){
[self.leftTitleArr replaceObjectAtIndex:indexPath.row withObject: @"恢復(fù)關(guān)注"];
WMMyFriendVCCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.fanStr = self.leftTitleArr[indexPath.row];
//action.backgroundColor = [UIColor orangeColor];
}
[self.tableView reloadData];
}];
UITableViewRowAction *actionRight = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
if (indexPath.section == 1) {
[self.dataSource removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}
}];
return @[actionRight, actionLeft];
}else{
return @[];
}
}
//自定義的cell中重寫(xiě)一個(gè)系統(tǒng)方法來(lái)添加子控件,并且在頭文件中提供一個(gè)NSString *fanStr的東西.
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index {
[super insertSubview:view atIndex:index];
if (![self.fanStr isEqualToString:@"恢復(fù)關(guān)注"]) {
return;
}
if ([view isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
for (int i = 0; i < view.subviews.count; i ++) {
UIButton *btn = view.subviews[i];
if ([btn isKindOfClass:[UIButton class]] && i > 0) {
[btn setBackgroundColor:[UIColor orangeColor]];
[btn setTitle:self.fanStr forState:UIControlStateNormal];
[btn setTintColor:[UIColor whiteColor]];
}
}
}
}
4.補(bǔ)充1:如果項(xiàng)目只需要實(shí)現(xiàn)左滑刪除功能,可以通過(guò)下面兩個(gè)方法組合實(shí)現(xiàn),簡(jiǎn)單.
/**
* 只要實(shí)現(xiàn)這個(gè)方法,就擁有系統(tǒng)默認(rèn)的左滑刪除功能(默認(rèn))
* 點(diǎn)擊系統(tǒng)默認(rèn)的 Delete按鈕 會(huì)來(lái)到這個(gè)方法
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.dataArray removeObjectAtIndex:indexPath.row];
//刷新模型數(shù)據(jù)
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
//修改默認(rèn)Delete按鈕的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"刪除";
}
5.補(bǔ)充2:實(shí)現(xiàn)點(diǎn)擊即可左滑刪除

Paste_Image.png
//下面這句代碼不能少
self.tableView.edting = YES;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
//觸發(fā)事件后會(huì)來(lái)到下面方法,在該方法中實(shí)現(xiàn)對(duì)應(yīng)的代碼即可.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
如果我的分享對(duì)你有一絲絲啟發(fā),那就給我個(gè)吧,權(quán)當(dāng)鼓勵(lì)了,如果有什么疑問(wèn),可以留言,謝謝你的閱讀.