iOS 8之前使用如下方式自定義UITableView左劃后顯示的文字,不過該樣式太單一了,而且只能顯示一個:
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
而從iOS 8開始UITableView的協議UITableViewDelegate增加了如下方法,可以自定義左劃后顯示多個,樣式也有更多選擇了:
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
實現該代理方法后,以下的這個代理方法就不執行了:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
UITableViewRowAction的定義如下:
NS_CLASS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED @interface UITableViewRowAction : NSObject <NSCopying>+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;
@property (nonatomic, readonly) UITableViewRowActionStyle style;@property (nonatomic, copy, nullable) NSString *title;@property (nonatomic, copy, nullable) UIColor *backgroundColor; // default background color is dependent on style@property (nonatomic, copy, nullable) UIVisualEffect* backgroundEffect;@end
typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { UITableViewRowActionStyleDefault = 0, UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault, UITableViewRowActionStyleNormal} NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
其中不修改backgroundColor時,backgroundColor的顏色是由style決定的,UITableViewRowActionStyleDestructive時是紅色的刪除樣式,UITableViewRowActionStyleNormal時是灰色樣式,類似于微信好友列表左劃后的“備注”。
| 
 
 | 
新聞熱點
疑難解答