首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何模糊UITableViewCell

如何模糊UITableViewCell
EN

Stack Overflow用户
提问于 2015-07-06 13:11:53
回答 2查看 1.2K关注 0票数 1

我用一张表向用户展示图书目录。有些书是免费的,另一些是高级书。我想通知用户,根据他们的帐户状态(免费或溢价),他们可以下载的书籍。尽管如此,用户仍然可以检查所有的书籍(封面和摘要,但不下载)。

为了达到这个目的,我正在尝试淡化包含免费用户高级书籍的单元格。

我怎么能让细胞变暗?我已经尝试过以前的答案,如How do I make a UITableViewCell appear disabled?UITableview: How to Disable Selection for Some Rows but Not Others,但没有运气。为什么阿尔法不行?

我的尝试朝这个方向发展,但没有结果:

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-06 13:45:55

请参阅/阅读目标C中关于Type Casting的内容。

铸造NSNumber(类)和NSInteger(原语类型)的区别。

这里有一个样本:

代码语言:javascript
复制
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

代码语言:javascript
复制
- (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;
}

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

票数 1
EN

Stack Overflow用户

发布于 2015-07-06 13:17:14

编辑:先试试这个!

代码语言:javascript
复制
cell.contentView.alpha = 0.2

而不是

代码语言:javascript
复制
cell.alpha = 0.2

我在我的项目上进行了测试,唯一的一个项目运行良好。

如果它不起作用,那么也试试下面的.

我想这条线有问题

代码语言:javascript
复制
if (cell == nil) {
    cell = [[SelectBookCell alloc] init];
}

把它改成

代码语言:javascript
复制
if (cell == nil) {
    cell = [[SelectBookCell alloc] initWithNibName:@"SelectBookCell" bundle:nil]
}

请注意,xib文件名@"SelectBookCell“可能与您的项目不同。我不知道你的手机在故事板或xib文件中。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31246829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档