【问题标题】:How do I make a UITableViewCell appear disabled?如何使 UITableViewCell 显示为禁用?
【发布时间】:2011-08-19 19:43:29
【问题描述】:

我知道UITableview: How to Disable Selection for Some Rows but Not Otherscell.selectionStyle = UITableViewCellSelectionStyleNone,但是如何使单元格(或任何UIView)显示为如下所示的禁用(灰显)?

【问题讨论】:

    标签: uitableview appearance


    【解决方案1】:

    您可以禁用单元格的文本字段以使其变灰:

    Swift 4.x

    cell!.isUserInteractionEnabled = false
    cell!.textLabel!.isEnabled = false
    cell!.detailTextLabel!.isEnabled = false
    

    【讨论】:

    • 或者更短一点:cell.userInteractionEnabled = cell.textLabel.enabled = cell.detailTextLabel.enabled = NO;
    • 更短但更丑
    【解决方案2】:

    在我使用的上下文中运行良好的 Swift 扩展;你的旅费可能会改变。

    Swift 2.x

    extension UITableViewCell {
        func enable(on: Bool) {
            for view in contentView.subviews as! [UIView] {
                view.userInteractionEnabled = on
                view.alpha = on ? 1 : 0.5
            }
        }
    }
    

    斯威夫特 3:

    extension UITableViewCell {
        func enable(on: Bool) {
            for view in contentView.subviews {
                view.isUserInteractionEnabled = on
                view.alpha = on ? 1 : 0.5
            }
        }
    }
    

    现在只需致电myCell.enable(truthValue)

    【讨论】:

      【解决方案3】:

      感谢@Ajay Sharma,我想出了如何使UITableViewCell 出现禁用:

      // Mac's native DigitalColor Meter reads exactly {R:143, G:143, B:143}.
      cell.textLabel.alpha = 0.439216f; // (1 - alpha) * 255 = 143
      aSwitch.enabled = NO; // or [(UISwitch *)cell.accessoryView setEnabled:NO];
      

      然后,实际上禁用单元格:

      cell.userInteractionEnabled = NO;
      

      【讨论】:

      • 是的,当然,你也可以这样做,通过设置 alpha :)
      【解决方案4】:

      尝试使用一个小技巧:

      只需设置单元格的 alpha。将一些条件作为您自己的要求并设置 alpha。

      cell.alpha=0.2;
      

      如果它不起作用,那么你喜欢它的方式,使用第二个技巧,

      只需拍摄具有灰色背景和透明背景的单元格大小的图像,只需将该图像添加到单元格内容的图像中即可。 像这样:

      // Customize the appearance of table view cells.
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          static NSString *CellIdentifier = @"Cell";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
              cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
          }
      
          // Configure the cell...
      
      
          if(indexPath.row==0)
          {
              cell.userInteractionEnabled=FALSE;
      
              UIImageView *img=[[UIImageView alloc]init];
              img.frame=CGRectMake(0, 0, 320, 70);
              img.image=[UIImage imageNamed:@"DisableImage.png"];
              img.backgroundColor=[UIColor clearColor];
              [cell.contentView addSubview:img];
              [img release];
      
          }
          else {
              //Your usual code for cell interaction.
      
          }
          return cell;
      }
      

      虽然我不确定方法,但这肯定会满足您的要求。这会在用户心中产生一种细胞已禁用的错觉。 请尝试使用此解决方案。希望能解决您的问题。

      【讨论】:

      • cell.alpha=0.2 - 对我不起作用。 cell.ContentView.Alpha = 0.2 为我工作
      • 这里设置 cell.alpha - 不起作用,设置 cell.ContentView.alpha 就可以了
      【解决方案5】:

      Swift 4.X

      来自 Kevin Owens 的不错的扩展, 我正在纠正细胞的行为。

      extension UITableViewCell {
          func enable(on: Bool) {
              self.isUserInteractionEnabled = on
              for view in contentView.subviews {
                  self.isUserInteractionEnabled = on
                  view.alpha = on ? 1 : 0.5
              }
          }
      }
      

      如何称呼它:-

      cell.enable(on: switch.isOn)

      【讨论】:

        【解决方案6】:

        Kevin Owens 的出色扩展,这是我对使用 Swift 2.x 的更正:

        extension UITableViewCell {
            func enable(on: Bool) {
                self.userInteractionEnabled = on
                for view in contentView.subviews {
                    view.userInteractionEnabled = on
                    view.alpha = on ? 1 : 0.5
                }
            }
        }
        

        斯威夫特 3:

        extension UITableViewCell {
            func enable(on: Bool) {
                self.isUserInteractionEnabled = on
                for view in contentView.subviews {
                    view.isUserInteractionEnabled = on
                    view.alpha = on ? 1 : 0.5
                }
            }
        }
        

        【讨论】:

        • 在 Swift 3.0 中使用 .isUserInteractionEnabled
        【解决方案7】:

        我创建了以下扩展来启用/禁用 UITableViewCell,使用起来非常方便。 使用“UITableViewCell+Ext.h”创建 UITableViewCell 扩展,其中包含以下内容。

        @interface UITableViewCell (Ext)
        
        - (void)enableCell:(BOOL)enabled withText:(BOOL)text;
        - (void)enableCell:(BOOL)enabled withText:(BOOL)text withDisclosureIndicator:(BOOL)disclosureIndicator;
        - (void)disclosureIndicator:(BOOL)disclosureIndicator;
        
        @end
        

        “UITableViewCell+Ext.m”包含以下内容。

        @implementation UITableViewCell (Ext)
        
        - (UITableView *)uiTableView {
            if ([[UIDevice currentDevice] systemVersionIsGreaterThanOrEqualTo:@"7.0"]) {
                return (UITableView *)self.superview.superview;
            }
            else {
                return (UITableView *)self.superview;
            }
        }
        
        - (void)enableCell:(BOOL)enabled withText:(BOOL)text {
            if (enabled) {
                self.userInteractionEnabled = YES;
        
                if (text) {
                    self.textLabel.alpha = 1.0f;
                    self.alpha = 1.0f;
                    self.detailTextLabel.hidden = NO;
                }
            }
            else {
                self.userInteractionEnabled = NO;
        
                if (text) {
                    self.textLabel.alpha = 0.5f;
                    self.alpha = 0.5f;
                    self.detailTextLabel.hidden = YES;
                }
            }
        }
        
        - (void)enableCell:(BOOL)enabled withText:(BOOL)text withDisclosureIndicator:(BOOL)disclosureIndicator {
            if (enabled) {
                self.userInteractionEnabled = YES;
        
                if (text) {
                    self.textLabel.alpha = 1.0f;
                    self.alpha = 1.0f;
                    self.detailTextLabel.hidden = NO;
                }
        
                self.accessoryType = disclosureIndicator ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
            }
            else {
                self.userInteractionEnabled = NO;
        
                if (text) {
                    self.textLabel.alpha = 0.5f;
                    self.alpha = 0.5f;
                    self.detailTextLabel.hidden = YES;
                }
        
                self.accessoryType = UITableViewCellAccessoryNone;
            }
        }
        
        - (void)disclosureIndicator:(BOOL)disclosureIndicator {
            if (disclosureIndicator) {
                self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            else {
                self.accessoryType = UITableViewCellAccessoryNone;
            }
        }
        
        @end
        

        如何禁用单元格:

        [cell enableCell:NO withText:NO];
        
        [cell enableCell:NO withText:YES withDisclosureIndicator:YES];
        

        如何启用单元格:

        [cell enableCell:YES withText:NO];
        
        [cell enableCell:YES withText:YES withDisclosureIndicator:YES];
        

        希望对你有所帮助。

        【讨论】:

          【解决方案8】:

          快速

          cell.isUserInteractionEnabled = false
          

          【讨论】:

          • 这不会影响单元格的外观。
          【解决方案9】:

          Swift 5 版本

          class CustomCell: UITableViewCell {
              
              private func isEnabled(_ enabled: Bool) {
                  isUserInteractionEnabled = enabled
                  subviews.forEach { subview in
                      subview.isUserInteractionEnabled = enabled
                      subview.alpha = enabled ? 1 : 0.5
                  }
              }
              
          }
              
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-24
            • 1970-01-01
            • 2017-09-11
            • 1970-01-01
            • 2015-04-16
            相关资源
            最近更新 更多