Ios 如何调暗UITableViewCell

Ios 如何调暗UITableViewCell,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我使用表格向用户显示图书目录。有些书是免费的,有些是高级书。我想根据用户的帐户状态(免费或高级)通知用户可以下载的书籍。不过,用户可以查看所有书籍(封面和摘要,但不能下载) 出于这个目的,我正试图淡化包含免费用户的高级书籍的单元格 我如何使细胞变暗?我已经尝试过以前的答案,比如和,但没有运气。为什么alpha不起作用 我的尝试朝着这个方向进行,但没有结果: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIn

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

出于这个目的,我正试图淡化包含免费用户的高级书籍的单元格

我如何使细胞变暗?我已经尝试过以前的答案,比如和,但没有运气。为什么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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellIdentifier=@“SelectBookCell”;
选择BookCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(单元格==nil){
cell=[[SelectBookCell alloc]init];
}
//配置要显示的图像、标题、作者…信息
做事
//暗淡的高级书籍

如果([[userAccount level]intValue]编辑:请先尝试此操作!

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文件中。

编辑:首先尝试此操作!

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文件中。

请查看/阅读有关目标C中的
类型转换的信息。
铸造
NSNumber
(类)和
NSInteger
(基本类型)之间的差异

以下是一个示例:

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]);

关于你的问题:

由于您使用的是默认视图
.textlab
&
.detailtextlab
,因此我确信您必须使用
单元格.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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellIdentifier=@“SelectBookCell”;
选择BookCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(单元格==nil)
{
//这是错误的
//cell=[[SelectBookCell alloc]init];
// --
//一定是
cell=[[SelectBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
...
//这句话看起来是正确的,但如果你检查一下,就不正确了
//尝试记录以下内容:
NSLog(@“错误:%@),(1<(NSInteger)@“1”)?@“是”:“否”);
//这将返回'YES',这是错误的
//如果您尝试以下方法:
NSLog(@“Correct:%@”,(1<[1”intValue])?@“YES”:@“NO”);
//它将记录正确的日志
//你的情况

//`if([[userAccount level]intValue]请查看/阅读目标C中的
类型转换

铸造
NSNumber
(类)和
NSInteger
(基本类型)之间的差异

以下是一个示例:

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]);

关于你的问题:

由于您使用的是默认视图
.textlab
&
.detailtextlab
,因此我确信您必须使用
单元格.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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellIdentifier=@“SelectBookCell”;
选择BookCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(单元格==nil)
{
//这是错误的
//cell=[[SelectBookCell alloc]init];
// --
//一定是
cell=[[SelectBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
...
//这句话看起来是正确的,但如果你检查一下,就不正确了
//尝试记录以下内容:
NSLog(@“错误:%@),(1<(NSInteger)@“1”)?@“是”:“否”);
//这将返回'YES',这是错误的
//如果您尝试以下方法:
NSLog(@“Correct:%@”,(1<[1”intValue])?@“YES”:@“NO”);
//它将记录正确的日志
//你的情况

//`如果([[userAccount level]intValue]是您的
SelectBookCell
自定义类的
UITableViewCell
?脚本中的main?是您的
SelectBookCell
自定义类的
UITableViewCell
?脚本中的main?类型转换已检查并理解:)@kitimenpolku.干杯!:)已检查并理解类型转换:)@kitimenpolku.干杯!:)