我正在使用表格向用户显示图书目录。有些书是免费的,有些是付费书。我想根据用户的帐户状态(免费或高级)通知用户他们可以下载的书籍。尽管如此,用户仍可以查看所有书籍(封面和摘要但不能下载)。
为此,我试图调暗包含免费用户付费图书的单元格。
如何调暗单元格?我已经尝试过以前的答案,例如 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/