UITableView中的阴影不一致

时间:2013-03-18 12:12:04

标签: iphone ios ipad uitableview

我将shadows添加到UITableView的单个单元格中。阴影在某种意义上并不一致,请考虑以下情况:

要显示20行,最初在第一个视图中只有10行可见。阴影可以按预期正确显示。但是当我向下/向上滚动时,现在可以看到的一些新细胞正在显示预期的阴影,而其他细胞则没有。该问题与zPosition的{​​{1}}的{​​{1}}一致。对于某些细胞而言,阴影位于后方,而对于其他细胞而言,阴影位于其前方,而位于其下方的细胞则使其对用户可见/不可见。

因为,我遇到的大多数帖子(例如。Objective C: How to add Shadow effect to navigation bar and table cells)没有明确设置UITableViewCell的图层的zPosition所以我想知道这是否是必需的或者我有什么在这里失踪。

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

layer

1 个答案:

答案 0 :(得分:0)

尝试这样可能有效,

    -(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;
            }