如何在 Objective-C 的 UITableView 中使 UITableViewRowAction 完全透明

Posted

技术标签:

【中文标题】如何在 Objective-C 的 UITableView 中使 UITableViewRowAction 完全透明【英文标题】:How to make UITableViewRowAction Completely Transparent in UitableView in Objective-C 【发布时间】:2018-09-17 13:51:13 【问题描述】:

我想让 UITableViewRowAction 透明并且我的背景视图应该在我的 UITableViewCell 在 Objective-C 中向左滑动时可见。我已经清除了所有背景颜色,但背景仍然是浅灰色。

这是我的代码:

ViewController.h

#import <UIKit/UIKit.h>


    @interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    
    
    
    @property (nonatomic, strong) UITableView *MyCustomTableView;
    @property (nonatomic, strong)NSArray *MyTableViewData;
    @property (nonatomic, strong)UIImage *MyAppIconImage;
    @end

ViewController.m

#import "ViewController.h"
#import "MyTableViewCell.h"

static NSString *myCellID = @"myCellIdentifier";
@interface ViewController ()

@end

@implementation ViewController

@synthesize MyCustomTableView, MyTableViewData, MyAppIconImage;
- (void)viewDidLoad 
    [super viewDidLoad];
    UIGraphicsBeginImageContext(self.view.frame.size);
    [[UIImage imageNamed:@"SampleBg"] drawInRect:self.view.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.view.backgroundColor = [UIColor colorWithPatternImage:image];

    self.MyTableViewData = @[@@"Title" : @"My TableView Title 1", @"Description" : @"My TableView Description 1", @@"Title" : @"My TableView Title 2", @"Description" : @"My TableView Description 2", @@"Title" : @"My TableView Title 3", @"Description" : @"My TableView Description 3", @@"Title" : @"My TableView Title 4", @"Description" : @"My TableView Description 4", @@"Title" : @"My TableView Title 5", @"Description" : @"My TableView Description 5", @@"Title" : @"My TableView Title 6", @"Description" : @"My TableView Description 6", @@"Title" : @"My TableView Title 7", @"Description" : @"My TableView Description 7", @@"Title" : @"My TableView Title 8", @"Description" : @"My TableView Description 8"];
    
    
    
UIImage *AppIconAsset = [UIImage imageNamed: [[NSBundle mainBundle].infoDictionary[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"] lastObject]];
    self.MyAppIconImage = [self ResizeImage: AppIconAsset scaledToSize: CGSizeMake(36, 36)];
    
    self.MyCustomTableView = [UITableView new];
    self.MyCustomTableView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview: self.MyCustomTableView];
    self.MyCustomTableView.dataSource = self;
    self.MyCustomTableView.delegate = self;
    [self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[MyCustomTableView]|" options: 0 metrics: nil views: @@"MyCustomTableView" : self.MyCustomTableView]];
    
    [self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[MyCustomTableView]|" options: 0 metrics: nil views: @@"MyCustomTableView" : self.MyCustomTableView]];
    MyCustomTableView.backgroundColor = [UIColor clearColor];
    
    [self.MyCustomTableView registerClass: [MyTableViewCell class].self forCellReuseIdentifier: myCellID];
    self.MyCustomTableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0);
    self.MyCustomTableView.allowsMultipleSelectionDuringEditing = NO;
    self.MyCustomTableView.alwaysBounceVertical = NO;
    [self.MyCustomTableView setShowsHorizontalScrollIndicator:NO];
    [self.MyCustomTableView setShowsVerticalScrollIndicator:NO];
    [self.MyCustomTableView setContentOffset: self.MyCustomTableView.contentOffset animated: NO];
    self.MyCustomTableView.estimatedRowHeight = 44;
    self.MyCustomTableView.backgroundView = nil;
   // self.MyCustomTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.MyCustomTableView setSeparatorColor:[UIColor blackColor]];
    self.MyCustomTableView.tableFooterView = [[UIView alloc] initWithFrame: CGRectZero];
    [self.MyCustomTableView.inputAccessoryView setBackgroundColor:[UIColor clearColor]];
    self.MyCustomTableView.opaque = NO;



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    return  1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return  [self.MyTableViewData count];


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    
    MyTableViewCell *myCell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier: myCellID forIndexPath: indexPath];
    if (myCell == nil) 
        myCell = [[MyTableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: myCellID];
    
    
    myCell.MyCellImageView = [UIImageView new];
    myCell.MyCellImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [myCell.contentView addSubview: myCell.MyCellImageView];
    [myCell.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-12-[MyCellImageView(34)]" options: 0 metrics: nil views: @@"MyCellImageView": myCell.MyCellImageView]];
    [myCell.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-5-[MyCellImageView(34)]" options: 0 metrics: nil views: @@"MyCellImageView": myCell.MyCellImageView]];
    
   
    
    
    myCell.MyCellImageView.image = self.MyAppIconImage;
    myCell.MyCellImageView.backgroundColor = [UIColor clearColor];

    myCell.MyCellImageView.contentMode = UIViewContentModeCenter;
    myCell.MyCellImageView.userInteractionEnabled = NO;
    myCell.MyCellImageView.clipsToBounds = YES;
    myCell.MyCellImageView.layer.cornerRadius = 17;
    
    
    myCell.MyCellTitleLabel = [UILabel new];
    myCell.MyCellDescriptionLabel = [UILabel new];
    myCell.MyCellTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    myCell.MyCellDescriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
    [myCell.contentView addSubview: myCell.MyCellTitleLabel];
    [myCell.contentView addSubview: myCell.MyCellDescriptionLabel];
    
    [myCell.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:[MyCellImageView]-10-[MyCellTitleLabel]-10-|" options: 0 metrics: nil views: @@"MyCellTitleLabel" : myCell.MyCellTitleLabel, @"MyCellImageView": myCell.MyCellImageView]];
    [myCell.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:[MyCellImageView]-10-[MyCellDescriptionLabel]-10-|" options: 0 metrics: nil views: @@"MyCellDescriptionLabel" : myCell.MyCellDescriptionLabel, @"MyCellImageView": myCell.MyCellImageView]];
    [myCell.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-10-[MyCellTitleLabel]" options: 0 metrics: nil views: @@"MyCellTitleLabel" : myCell.MyCellTitleLabel]];
    
    [myCell.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:[MyCellTitleLabel]-(5)-[MyCellDescriptionLabel]-10-|" options: 0 metrics: nil views: @@"MyCellTitleLabel" : myCell.MyCellTitleLabel, @"MyCellDescriptionLabel" : myCell.MyCellDescriptionLabel]];
    
    
     myCell.MyCellTitleLabel.text = [[self.MyTableViewData objectAtIndex:indexPath.row] valueForKey: @"Title"];
    
    myCell.MyCellDescriptionLabel.text = [[self.MyTableViewData objectAtIndex:indexPath.row] valueForKey: @"Description"];
    myCell.MyCellDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
    myCell.MyCellDescriptionLabel.numberOfLines = 0;
    
    myCell.MyCellDescriptionLabel.textAlignment = NSTextAlignmentJustified;
    myCell.MyCellDescriptionLabel.backgroundColor = [UIColor clearColor];
    
    myCell.backgroundColor = [UIColor clearColor];
    myCell.backgroundView.backgroundColor = [UIColor clearColor];
    myCell.backgroundView.opaque = NO;
    myCell.backgroundView = nil;
    myCell.textLabel.textColor = [UIColor blackColor];
    return myCell;




- (NSArray<UITableViewRowAction *> *)tableView: (UITableView *)tableView editActionsForRowAtIndexPath: (NSIndexPath *)indexPath

    MyTableViewCell *myCell = [MyCustomTableView cellForRowAtIndexPath: indexPath];
    [myCell.contentView layoutIfNeeded];
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title: @"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) 
        
        NSLog(@"rowAction Performed !!");
    ];
    rowAction.backgroundColor = [UIColor clearColor];
    return @[rowAction];



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
        //  [self.objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade];
     else if (editingStyle == UITableViewCellEditingStyleInsert) 
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    



-(UIImage*)ResizeImage:(UIImage*)image scaledToSize:(CGSize)newSize 
    
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

MyTableViewCell.h

#import <UIKit/UIKit.h>

@interface MyTableViewCell : UITableViewCell
@property (nonatomic, strong) UILabel *MyCellTitleLabel;
@property (nonatomic, strong)UILabel *MyCellDescriptionLabel;
@property (nonatomic, strong)UIImageView *MyCellImageView;

@end

MyTableViewCell.m

#import "MyTableViewCell.h"

@implementation MyTableViewCell

- (void)awakeFromNib 
    [super awakeFromNib];
    // Initialization code


-(id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier 
    self = [super initWithStyle:style reuseIdentifier: reuseIdentifier];
    if (self) 
        self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        
        
        //    self.nvFullScrnPlayerDelegate = self;
    
    return self;


- (void)prepareForReuse 
    [super prepareForReuse];
    
    for(UIView *subview in [self.contentView subviews]) 
        [subview removeFromSuperview];
    


-(void)layoutSubviews 
    [super layoutSubviews];
//    for (UIView *subview in  self.subviews) 
//        for (UIView *subview2 in subview.subviews) 
//
////NSRange range = ;
//            if ([(NSString *)subview2 rangeOfString:@"UITableViewCellActionButton"].location != NSNotFound) 
//                NSLog(@"1234567890");
//            
////            if (range.location != NSNotFound) 
////                for (UIView *view in  subview2.subviews) 
////
////NSRange newRange = [(NSString *)view rangeOfString: @"UIButtonLabel"];
//////     if (String(view).rangeOfString("UIButtonLabel") != nil) 
////if (newRange.location != NSNotFound) 
////    UILabel *Textlabel = (UILabel *)view;
////    if (Textlabel == (UILabel *) view) 
////        Textlabel.textColor = [UIColor blackColor];
////    
////                    
////                
////            
//
//        
//          


@end

这是我的结果:

在同一个表格视图单元格上第二次滑动时,我得到了预期的结果,但不是在第一次滑动时。

谁能帮我解决一下?

【问题讨论】:

试试这个方法 在editActionsForRowAtIndexPath MyTableViewCell *myCell = (MyTableViewCell *) [tableView cellForRowAtIndexPath:indexPath];和 rowAction.backgroundColor = [UIColor clearColor];供参考:-qaru.site/questions/94631/… 在 cellForRowAtIndexPath 中为每个单元格添加标签,例如 myCell.tag = indexPath.row; 相同的灰色背景在滑动时没有被移除。我想在滑动单元格删除时永久删除浅灰色背景。 【参考方案1】:

希望对您有所帮助!

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

    return YES;


- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath  
  
    UITableViewRowAction *callAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)   

        NSLog(@"View Action fired");  
    ];  
    callAction.backgroundColor = [UIColor clearColor];  

    return @[callAction];  

【讨论】:

试试这个@MohammadAshrafAli 并告诉我! 我再次测试过,这在 ios 10 中运行良好,但在 iOS 11 或更高版本的设备中出现了灰色背景。并附上 iPhoneX 模拟器截图。【参考方案2】:

如果你设置清晰的颜色意味着它显示浅灰色。你可以设置SampleBg图像而不是清晰的颜色。从图像中你可以获得相同的tableview背景颜色试试下面的代码。它会工作

- (NSArray<UITableViewRowAction *> *)tableView: (UITableView *)tableView editActionsForRowAtIndexPath: (NSIndexPath *)indexPath


    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) 

        NSLog(@"rowAction Performed !!");
    ];
    UIGraphicsBeginImageContext(self.view.frame.size);
    [[UIImage imageNamed:@"SampleBg"] drawInRect:self.view.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    rowAction.backgroundColor = [UIColor colorWithPatternImage:image];
    return @[rowAction];

【讨论】:

嗨,Rajesh,它正在从左上角绘制主视图的图像,因此由于主图像中的渐变色,该图像看起来像不同的图像

以上是关于如何在 Objective-C 的 UITableView 中使 UITableViewRowAction 完全透明的主要内容,如果未能解决你的问题,请参考以下文章

Objective-C 字典键按降序排序

如何将文本包装在 Iphone 的 UITable 单元格中?

如何在代码中使用 UItable 单元中的按钮插座

如何在 UITable 中实现 Instagram 评论的效果?

AFNetworking JSON 到 UITableView - Objective-C

如何在 swift 3 中为 UITable 视图提供单元格缩进级别?