我用一张表向用户展示图书目录。有些书是免费的,另一些是高级书。我想通知用户,根据他们的帐户状态(免费或溢价),他们可以下载的书籍。尽管如此,用户仍然可以检查所有的书籍(封面和摘要,但不下载)。
为了达到这个目的,我正在尝试淡化包含免费用户高级书籍的单元格。
我怎么能让细胞变暗?我已经尝试过以前的答案,如How do I make a UITableViewCell appear disabled?和UITableview: How to Disable Selection for Some Rows but Not Others,但没有运气。为什么阿尔法不行?
我的尝试朝这个方向发展,但没有结果:
- (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;
}发布于 2015-07-06 13:45:55
请参阅/阅读目标C中关于Type Casting的内容。
铸造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]);关于你的问题:
由于您使用的是默认视图.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;
}希望这有帮助..。干杯。
发布于 2015-07-06 13:17:14
编辑:先试试这个!
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文件中。
https://stackoverflow.com/questions/31246829
复制相似问题