如何使UITableViewCell显示为禁用?

ma1*_*w28 85 appearance uitableview

我知道的UITableView:如何选择禁用一些行,但不是别人cell.selectionStyle = UITableViewCellSelectionStyleNone,但如何使一个单元格(或任何UIView与此有关)会被禁用(变灰)象下面这样?

禁用UITableViewCell

Sym*_*ric 158

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

Swift 4.x

cell!.isUserInteractionEnabled = false
cell!.textLabel!.isEnabled = false
cell!.detailTextLabel!.isEnabled = false
Run Code Online (Sandbox Code Playgroud)

  • 虽然更短但更好 (44认同)
  • 或者更短:`cell.userInteractionEnabled = cell.textLabel.enabled = cell.detailTextLabel.enabled = NO;` (17认同)

ma1*_*w28 22

感谢@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];
Run Code Online (Sandbox Code Playgroud)

然后,实际禁用单元格:

cell.userInteractionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)


Kev*_*ens 22

一个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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3:

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

现在只需要打电话myCell.enable(truthValue).


Aja*_*rma 18

尝试使用小技巧:

只需设置单元格的alpha即可.把一些条件作为你自己的要求并设置alpha.

cell.alpha=0.2;
Run Code Online (Sandbox Code Playgroud)

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

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

// 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;
}
Run Code Online (Sandbox Code Playgroud)

虽然我对这种方式并不确定,但这肯定会满足你的要求.这会给用户带来一种错觉,即单元格是禁用的.只是尝试使用此解决方案.希望能解决您的问题.

  • cell.alpha = 0.2 - 对我不起作用.cell.ContentView.Alpha = 0.2为我工作 (5认同)

Ash*_*shu 5

斯威夫特 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何称呼它:-

cell.enable(on: switch.isOn)