【问题标题】:How to dim UITableViewCell如何使 UITableViewCell 变暗
【发布时间】:2015-09-23 15:47:16
【问题描述】:

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

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

如何使单元格变暗?我已经尝试过以前的答案,例如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;
}

【问题讨论】:

  • 您的SelectBookCellUITableViewCell 的自定义类吗?故事板中的主要内容?

标签: ios objective-c uitableview


【解决方案1】:

编辑:先试试这个!

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" 可能不同。我不知道您的单元格在故事板或 xib 文件中。

【讨论】:

    【解决方案2】:

    请查看/阅读目标 C 中的 Type Casting
    转换 NSNumber(class) 和 NSInteger(primitive type) 之间的区别。

    这里是一个示例:

    NSLog(@"Wrong#1 : %@, Value: %d", (1 == ((int)@"1")) ? @"YES" : @"NO", ((int)@"1"));
    
    NSLog(@"Wrong#2 : %@, Value: %d", (1 == ((NSInteger)@"1")) ? @"YES" : @"NO", ((NSInteger)@"1"));
    
    NSLog(@"Wrong#3 : %@, Value: %d", (1 == ((NSInteger)@1)) ? @"YES" : @"NO", ((NSInteger)@1));
    
    NSLog(@"Wrong#4 : %@, Value: %d", (1 == ((int)@1)) ? @"YES" : @"NO", ((int)@1));
    
    NSLog(@"Correct#1 : %@, Value: %d", (1 == [@"1" intValue]) ? @"YES" : @"NO", [@"1" intValue]);
    
    NSLog(@"Correct#2 : %@, Value: %d", (1 == [@"1" integerValue]) ? @"YES" : @"NO", [@"1" integerValue]);
    

    关于您的问题:

    由于您使用的是默认视图.textLabel.detailTextLabel,我确定您必须使用cell.contentView

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"SelectBookCell";
    
        SelectBookCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil) 
        {
            // this is wrong
            // cell = [[SelectBookCell alloc] init];
            // --
            // must be
            cell = [[SelectBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        ...
    
        // this statement looks correct but if you check it, it's not
        // try logging this:
        NSLog(@"Wrong: %@", (1 < (NSInteger)@"1") ? @"YES" : @"NO");
        // this will return `YES` and that is wrong
    
        // if you try this:
        NSLog(@"Correct: %@", (1 < [@"1" intValue]) ? @"YES" : @"NO");
        // it will log the correct one
    
        // your condition 
        // `if([[userAccount level] intValue] <= (NSInteger)[book objectForKey:@"level"])` 
        // was never true
        // try this is instead:
        if([[userAccount level] intValue] <= [[book objectForKey:@"level"] intValue])
        {
            [cell setAlpha: 0.2f];
            // or 
            [cell.contentView setAlpha: 0.2f];
        }
        else
        {
            // dont for get this else statement to avoid unwanted behaviour
            // on cell reuse
            [cell setAlpha:1.0f];
            // or 
            [cell.contentView setAlpha:1.0f];
        }
        return cell;
    }
    

    希望这有帮助...干杯。

    【讨论】:

    • 类型转换检查和理解现在:)
    • @kitimenpolku。干杯! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多