今天看我們的項目里有一個用第三方增加多個左滑選項的,他們說UITableView? 實現(xiàn)不了,我不信,然后我就去百度了。
后來發(fā)現(xiàn)百度倒是提到一個8.0以后UITableView增加的api
- (nullable NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
一句話帶過,也沒有說具體方法,然后我再搜出了第三方就沒有別的了,然后我就自己在UITableView 的代理方法里總結(jié)研究。
使用方法如下:
- (nullable NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *arrbtn = [[NSMutableArray alloc] init];
NSArray *title = @[@"delect",@"show",@"update"];
NSArray *color = @[[UIColor redColor],[UIColor grayColor],[UIColor greenColor]];
for (int a = 0; a <3; a++) {
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:title[a] handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
if (a == 0) {
[self tableView:tableView changeSelectStyle:UITableViewLeftSelectStyleDelect indexPath:indexPath];
}else if (a == 1)
{
[self tableView:tableView changeSelectStyle:UITableViewLeftSelectStyleShow indexPath:indexPath];
}
else if (a ==2)
[self tableView:tableView changeSelectStyle:UITableViewLeftSelectStyleUpdate indexPath:indexPath];
}];
action.backgroundColor = color[a];
[arrbtn addObject:action];
}
return? (NSArray *)arrbtn;
}
這就完成了創(chuàng)建,生成,返回點擊的效果,比第三方簡單多了,我看到在qq上使用的就是這種方法。
如果大家還不知道怎么寫可以參考下demo:多個左滑選項的demo
cocoachina下載路徑:http://code.cocoachina.com/view/135182?