gpt4 book ai didi

iphone - UITableView 中的阴影不一致

转载 作者:行者123 更新时间:2023-12-01 16:51:21 26 4
gpt4 key购买 nike

我正在添加 shadowsUITableView 的单个单元格.阴影在意义上是不一致的,请考虑以下场景:

有 20 行要显示,最初,在第一个 View 中只有 10 行可见。此处阴影按预期正确可见。但是,一旦我向下/向上滚动,现在可见的一些新单元格会按预期显示阴影,而其他单元格则不会。问题似乎与 zPosition 相关。的UITableViewCelllayer .对于某些单元格,阴影在后面,而对于其他单元格,阴影在前面,而位于其下方的单元格则使其对用户可见/不可见。

因为,我遇到的大多数帖子(例如 Objective C: How to add Shadow effect to navigation bar and table cells )都没有设置 UITableViewCell的图层的 zPosition 明确,所以我想知道这是必需的还是我在这里缺少的东西。

编辑:请在此处找到代码片段

    -(UITableViewCell*)tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
navigatorCell* cell = (navigatorCell *)[tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"] forIndexPath:indexPath];


// cell configuration code goes here

//now add shadow
[cell.layer setMasksToBounds:NO];
cell.layer.shadowColor = [[UIColor blackColor] CGColor];
cell.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
cell.layer.shadowRadius = 3.0f;
cell.layer.shadowOpacity = 0.750f;
cell.clipsToBounds = NO;
//if I uncomment this, then it works properly, but problem arises again if I insert/remove cells
// cell.layer.zPosition = -indexpath.row;

CGRect shadowFrame = cell.layer.bounds;
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGpath;
cell.layer.shadowPath = shadowPath;
return cell;
}

最佳答案

试试这样可能行得通,

    -(UITableViewCell*)tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
navigatorCell* cell = (navigatorCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

// cell configuration code goes here

//now add shadow
[cell.layer setMasksToBounds:NO];
cell.layer.shadowColor = [[UIColor blackColor] CGColor];
cell.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
cell.layer.shadowRadius = 3.0f;
cell.layer.shadowOpacity = 0.750f;
cell.clipsToBounds = NO;
//if I uncomment this, then it works properly, but problem arises again if I insert/remove cells
// cell.layer.zPosition = -indexpath.row;

CGRect shadowFrame = cell.layer.bounds;
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGpath;
cell.layer.shadowPath = shadowPath;
}
return cell;
}

关于iphone - UITableView 中的阴影不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com