搜索了一些我曾尝试将一些动态生成的单元格的背景设置为透明但没有成功的帖子。
在 Storyboard 中,我有一个原型(prototype)表,在它下面有一张图片。在 Storyboard 中,我已将原型(prototype)表的 alpha 设置为 0.3,以便图像部分可见。它根据需要呈现。
问题是单元格添加了额外的颜色层,因此图像仍然可见,但少了一点(例如 alpha 为 0.6)。实际上,好像是三层。表格、单元格(使背景变暗一点),然后是文本周围每行内的另一个小矩形(使背景变暗)。
我已经像这样编辑了生成单元格的函数(基于我从该主题的线程中了解到的内容):
NSString *cellIdentifier = @"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row]; // gets the text of the cell from an array
cell.textLabel.textColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0];
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
// I've also trued with = [UIColor colorWithRed:0. green:0 blue:0 alpha:0.0]
cell.backgroundView = backView;
问题是我仍然得到那个额外的层。
最佳答案
看,我已经对您的代码中的某处进行了更改并尝试使用它。
[tableView setOpaque:NO];
NSString *cellIdentifier = @"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//if cell is already created then reuse it no need to create unnecessarily.
if(cell= nil){//otherwise create new cells.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
}
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0];
关于ios - 单元格背景不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377419/