Ios UITableViewCell Z阶
我有一个包含单元格的uitableview。我想弄清楚如何调整单元格的z顺序。我试图做到这一点: 但是,当我加载tableview时,我得到以下结果: 我已经尝试了Ios UITableViewCell Z阶,ios,objective-c,xcode,uitableview,Ios,Objective C,Xcode,Uitableview,我有一个包含单元格的uitableview。我想弄清楚如何调整单元格的z顺序。我试图做到这一点: 但是,当我加载tableview时,我得到以下结果: 我已经尝试了[tableView sendSubViewToBack:cell]。同样,当我滚动到底部然后返回顶部时,结果与图1类似,但当我向下滚动时,结果与图2类似。我感谢你的帮助 static NSString *CellIdentifier = @"Cell"; NSString *row = [NSString stringW
[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;
不幸的是,你无法控制cellForRow。。。是被叫进来的,所以把手机发到后面或前面是不行的 您可能需要创建一个方法,该方法通过表的可见单元格,并根据索引路径对它们重新排序,但是您会弄乱无法控制的视图。表视图可以随时重新排列或添加子视图,您将尝试捕获所有这些事件
我将使用集合视图而不是表视图实现这样的视图,它允许您使用自定义布局为每个indexPath指定z位置,并允许您以不同的数量重叠单元格。创建自定义布局非常简单,尤其是对于表类型布局 它应该通过如下方式更改单元格层的zPosition来工作:
cell.layer.zPosition=CGFloat(indexPath.row)
在tableView:cellforrowatinexpath:
dataSource方法中。不要忘记将单元格的clipsToBound
属性设置为false 不久前,我也遇到过同样的问题
对我有效的解决方案是使用集合视图而不是表视图。通过从UICollectionViewFlowLayout扩展自定义类来创建自定义类,并在集合视图中使用它
class OverlappedCustomFlowLayout: UICollectionViewFlowLayout {
override func prepare() {
super.prepare()
// This allows us to make intersection and overlapping
// A negative number implies overlapping whereas positive implies space between the adjacent edges of two cells.
minimumLineSpacing = -100
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributes = super.layoutAttributesForElements(in: rect)
for currentLayoutAttributes: UICollectionViewLayoutAttributes in layoutAttributes! {
// zIndex - Specifies the item’s position on the z-axis.
// Unlike a layer's zPosition, changing zIndex allows us to change not only layer position,
// but tapping/UI interaction logic too as it moves the whole item.
currentLayoutAttributes.zIndex = currentLayoutAttributes.indexPath.row + 1
}
}
return layoutAttributes
}
附言——根据苹果公司的开发者文件
此属性用于确定项目的前后顺序
在布局期间。索引值较高的项目显示在项目顶部
具有较低的值。具有相同值的项具有未确定的
秩序。此属性的默认值为0
希望有帮助!:) 我在UITableViewCells上使用阴影时遇到了同样的问题,屏幕下方(Y方向)的单元格的阴影与屏幕顶部较高的单元格重叠 我的解决方案是基于indexPath在UITableViewCell层上设置Z位置
cell.layer.zPosition = CGFloat(indexPath.row)
我的问题只发生在个别章节中。如果后续部分的单元格也面临此问题,则需要将部分编号添加到计算中
cell.layer.zPosition = CGFloat(indexPath.section) * 1000.0 + CGFloat(indexPath.row)
如果每个部分中的行数超过1000行,请增加神奇数字,或者思考一下为什么应用程序中有1000行;-) 如果您希望使用降序
zPosition
s(以便第一个单元格位于顶部),请使用以下基于的代码。使用这种方法,无需手动重新计算zPosition
值
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
DispatchQueue.main.async {
for index in 0 ..< tableView.visibleCells.count {
let zPosition = CGFloat(tableView.visibleCells.count - index)
tableView.visibleCells[index].layer.zPosition = zPosition
}
}
}
func tableView(tableView:UITableView,didendisplaying单元格:UITableView单元格,forRowAt indexPath:indexPath){
DispatchQueue.main.async{
对于0中的索引.. <代码>你能显示你的代码吗?你尝试了什么?是的,我用我的当前代码更新了这个帖子。考虑使用集合视图。我们如何在DeDeCad上扩展任何单元格。我的意思是增加选中的一个的高度,并设置更改的动画。效果很好!我需要使用cell.layer.zPosition=CGFloat(arrstata.count-indexPath.row)
,但我从您那里得到了一些想法。顺便说一句,谢谢你。虽然这个解决方案乍一看很不错,但它有一个主要的警告:单元格将位于滚动指示器上方。由于滚动指示器位于分区标题下方,因此此解决方案存在问题。你可以在视图中循环查找滚动指示器并增大其Z值,但这可能会带来其他问题。我的应用程序中没有这个问题,因为我的单元格两边各有10个像素。快速修复方法是关闭滚动条。