ios - UITableViewCell Z 顺序

标签 ios objective-c xcode uitableview

我有一个包含单元格的 uitableview。我想弄清楚如何调整单元格的 z 顺序。我正在尝试这样做: enter image description here

但是当我加载 tableview 时,我得到了这个: enter image description here

我已经尝试过[tableView sendSubViewToBack:cell]。另外,当我滚动到底部然后返回顶部时,结果如图 1 所示,但是当我向下滚动时,结果如图 2 所示。感谢您的帮助。

    static NSString *CellIdentifier = @"Cell";
NSString *row = [NSString stringWithFormat:@"%li",(long)indexPath.row];
GetMoreTableViewCell *cell = [_chapters dequeueReusableCellWithIdentifier:CellIdentifier];
NSMutableDictionary *deckCategory = [[_data valueForKey:key] valueForKey:row];

if (cell == nil) {
    cell = [[GetMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


[tableView bringSubviewToFront:cell];

if([[[_data valueForKey:key] valueForKey:[NSString stringWithFormat:@"%i",indexPath.row]] valueForKey:@"isDefault"] == [NSNumber numberWithBool:YES]) {
    cell.addButton.hidden = YES;
}
cell.ttitle.text = [[[_data valueForKey:key] valueForKey:row] valueForKey:@"Name"];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CALayer * l = [cell.cellImage layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];

CALayer * b = [cell.cellBackground layer];
[b setMasksToBounds:YES];
[b setCornerRadius:19.0];
cell.cellImage.image = [UIImage imageNamed:[deckCategory valueForKey:@"Image"]];

if(indexPath.row == 0) {
    cell.cellBackground.image = [UIImage imageNamed:@"card_green.png"];
}
else if(indexPath.row == 1) {
    cell.cellBackground.image = [UIImage imageNamed:@"card_orange.png"];
}
else if(indexPath.row == 2) {
    cell.cellBackground.image = [UIImage imageNamed:@"card_red.png"];
}

return cell;

最佳答案

在我的 UITableViewCells 上使用阴影时,我遇到了同样的问题,屏幕下方的单元格(在 Y 方向)的阴影与屏幕顶部较高的单元格重叠。

我的解决方案是根据 indexPath 在 UITableViewCell 层上设置 Z 位置。

cell.layer.zPosition = CGFloat(indexPath.row)

我的问题只发生在个别部分。如果您后续部分的单元格也面临此问题,则您需要将部分编号添加到计算中。

cell.layer.zPosition = CGFloat(indexPath.section) * 1000.0 +  CGFloat(indexPath.row)

如果您在每个部分中有超过 1000 行,则增加魔数(Magic Number),或者考虑为什么您的应用程序中有 1000 行;-)

关于ios - UITableViewCell Z 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25718453/

相关文章:

ios - iOS 中如何查找 UIView 的 subview 的值变化

objective-c - 来自集合 : why does NSSet use allObjects, 的数组,而 NSOrderedSet 使用数组?

ios - 静态单元格的内容未出现在 UITableView 中

ios - IBInspectable 不适用于 Int 或 UInt

ios - Collection View performBatchUpdates 崩溃

ios - 由于获取核心数据对象时内存使用,应用程序卡住并终止

ios - @IBAction 执行Segue

swift - "canOpenURL"表现得很奇怪......或者我是?

ios - CollectionView prepareForSegue 不将图像数据发送到 DetailView

ios - Apple App Store 高级分析