ios - 如何调暗 UITableViewCell

标签 ios objective-c uitableview

我正在使用表格向用户显示图书目录。有些书是免费的,有些是付费书。我想根据用户的帐户状态(免费或高级)通知用户他们可以下载的书籍。尽管如此,用户仍可以查看所有书籍(封面和摘要但不能下载)。

为此,我试图调暗包含免费用户付费图书的单元格

如何调暗单元格?我已经尝试过以前的答案,例如 How do I make a UITableViewCell appear disabled?UITableview: How to Disable Selection for Some Rows but Not Others但没有运气。为什么 alpha 不起作用?

我在这个方向上的尝试没有结果:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SelectBookCell";

    SelectBookCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[SelectBookCell alloc] init];
    }

    // Config images, title, author... information to show
    Do stuff

    // Dimmed premium books
    if([[userAccount level] intValue] <= (NSInteger)[book objectForKey:@"level"])
    {
        [cell setAlpha: 0.2f];

        cell.textLabel.alpha = 0.2f;
        cell.detailTextLabel.alpha = 0.2f;
    }
    return cell;
}

最佳答案

编辑:先试试这个!

cell.contentView.alpha = 0.2

代替

cell.alpha = 0.2

我在我的项目上进行了测试,唯一的后一个工作正常。

如果它不起作用,请尝试以下方法。

我觉得这行有问题

if (cell == nil) {
    cell = [[SelectBookCell alloc] init];
}

改成

if (cell == nil) {
    cell = [[SelectBookCell alloc] initWithNibName:@"SelectBookCell" bundle:nil]
}

请注意,您的项目的 xib 文件名@"SelectBookCell"可能不同。我不知道您的单元格在 Storyboard或 xib 文件中。

关于ios - 如何调暗 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31246829/

相关文章:

ios - 初始化时出现 Firebase 错误 'NSInvalidArgumentException'

iphone - 在 iOS 应用程序中为 Library 的子目录打包文件

ios - 如何在 UITabBarController 中插入 UINavigationController

ios - Xcode - Swift - 文本的 UILabel 高度扩展

ios - 创建带有文本和十字符号的动态标签

iphone - imageView 的 iOS alpha 值比较失败

ios - 我应该在 cellForItemAtIndexPath 还是 willDisplayCell 上设置单元格?

ios - 以编程方式将内容添加到 UITableView 的顶部

ios - 删除 CoreData 对象

ios - 如何更新 UISegmentedControl