table:
- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *titleContent = @"? ? ? ? ? ? ? ? ? ? ? ? ? ? ";
UITableViewRowAction *callAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleContent handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//? ? ? ? self.main_table.editing = NO;
}];
UITableViewRowAction *messageAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleContent handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//? ? ? ? self.main_table.editing = NO;
}];
return @[messageAction, callAction];
}
cell:
// 這種方法是重寫willTransitionToState這個(gè)方法,而不是重寫layoutSubviews方法事期,因?yàn)樵谧蠡藛问掌饋淼臅r(shí)候宠纯,背景圖片會(huì)有閃一下的bug
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self setupSlideBtn];
});
}
// 設(shè)置左滑菜單按鈕的樣式
- (void)setupSlideBtn
{
// 判斷系統(tǒng)是否是iOS11及以上版本
if (@available(iOS 11.0, *)) {
for (UIView *subView in self.superview.subviews) {
if (![NSStringFromClass([subView class]) isEqualToString:@"UISwipeActionPullView"]) {
continue;
}
for (UIView *buttonViews in subView.subviews) {
if (![NSStringFromClass([buttonViews class]) isEqualToString:@"UISwipeActionStandardButton"]) {
continue;
}
// 修改備注
UIView *remarkContentView = subView.subviews[0];
[self setupRowActionView:remarkContentView imageName:@"b27_icon_star_yellow"];
// 刪除
UIView *deleteContentView = subView.subviews[1];
[self setupRowActionView:deleteContentView imageName:@"b27_icon_star_gray"];
}
}
} else {
// iOS11以下做法
for (UIView *subView in self.subviews) {
if(![subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
continue;
}
// SMS短訊
UIView *smsContentView = subView.subviews[0];
smsContentView.backgroundColor = BlueColor;
for (UIView *smsView in smsContentView.subviews) {
//移除界面所有子視圖? 注意區(qū)分視圖
[smsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
UIImageView *smsImage = [[UIImageView alloc] init];
smsImage.contentMode = UIViewContentModeScaleAspectFit;
smsImage.image = [UIImage imageNamed:@"b27_icon_star_yellow"];
CGFloat width = smsView.frame.size.width;
smsImage.frame = CGRectMake((width - 25) / 2, -15, 25, 25);
[smsView addSubview:smsImage];
// 文字
UILabel *sms = [self labelWithFrame:CGRectMake((width - 60) / 2, 40, 60, 20) text:@"哈哈哈哈哈" textColor:WhiteColor font:mainFontSize(14)];
sms.textAlignment = NSTextAlignmentCenter;
[smsView addSubview:sms];
}
// 訊息
UIView *messageContentView = subView.subviews[1];
messageContentView.backgroundColor = YellowColor;
for (UIView *messageView in messageContentView.subviews) {
[messageView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
UIImageView *messageImage = [[UIImageView alloc] init];
messageImage.contentMode = UIViewContentModeScaleAspectFit;
messageImage.image = [UIImage imageNamed:@"message_selected"];
CGFloat width = messageView.frame.size.width;
messageImage.frame = CGRectMake((width - 25) / 2, -15, 25, 25);
[messageView addSubview:messageImage];
// 文字
UILabel *message = [self labelWithFrame:CGRectMake((width - 35) / 2, 40, 35, 20) text:@"噗噗噗噗" textColor:WhiteColor font:mainFontSize(14)];
message.textAlignment = NSTextAlignmentCenter;
[messageView addSubview:message];
}
}
}
}
// 設(shè)置背景圖片
- (void)setupRowActionView:(UIView *)rowActionView imageName:(NSString *)imageName
{
rowActionView.backgroundColor = mainColor;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
[rowActionView insertSubview:imageView atIndex:0];
imageView.frame = CGRectMake(0, 0, 80, self.bounds.size.height);
}
-(UILabel*)labelWithFrame:(CGRect)rect text:(NSString *)text textColor:(UIColor*)color font:(UIFont*)font{
UILabel *label = [[UILabel alloc] initWithFrame:rect] ;
label.text = text;
label.textColor = color;
label.font = font;
return label;
}