【问题标题】:UITableViewCell inner shadow right side aloneUITableViewCell 内阴影右侧单独
【发布时间】:2014-04-21 09:56:57
【问题描述】:

我正在尝试仅在右侧为 UITableViewCell 创建内部阴影。我就是这样的,

if (![cell viewWithTag:100]) {
UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH, 0, 10, height)];
shadowView.layer.shadowColor = [UIColor darkGrayColor].CGColor;        
shadowView.layer.shadowRadius = 5.0;
shadowView.layer.shadowOffset = CGSizeMake(-2, 0);
shadowView.layer.shadowOpacity = 0.8;
shadowView.backgroundColor = [UIColor darkGrayColor];
shadowView.tag = 100;

shadowView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

[cell addSubview:self.shadowView];
}

但我的问题是每次滚动时阴影都会变暗。也会超出单元格边界并破坏单元格设计。我怀疑它会被反复添加。谁能帮我解决这个问题?我对使用图像作为阴影不感兴趣。因此,除了使用图像之外的任何其他解决方案都将受到赞赏。提前致谢。

【问题讨论】:

    标签: ios objective-c uitableview shadow


    【解决方案1】:

    每次滚动单元格刷新并再次添加视图时,您可以做两件事

    确保没有添加阴影并且您不会再次添加它。

    删除所有子视图并在创建单元格时重新添加它们

    【讨论】:

      【解决方案2】:

      确保您正确使用 dequeueReusableCellWithIdentifier。以下代码对我来说很好用

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      
          if (cell == nil)
          {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              NSLog(@"new cell");
      
                  UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 10, 44)];
                  shadowView.layer.shadowColor = [UIColor darkGrayColor].CGColor;
                  shadowView.layer.shadowRadius = 5.0;
                  shadowView.layer.shadowOffset = CGSizeMake(-2, 0);
                  shadowView.layer.shadowOpacity = 0.8;
                  shadowView.backgroundColor = [UIColor darkGrayColor];
                  shadowView.tag = 100;
                  shadowView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
      
                  [cell addSubview:shadowView];
          }
          else
          {
              NSLog(@"old cell");
          }
      
          return cell;
      }
      

      【讨论】:

        【解决方案3】:

        我发现了问题。我应该做的,

        self.contentView.superview.clipsToBounds = YES;
            self.contentView.clipsToBounds = YES;
        

        现在一切都像魅力一样运作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-06
          • 1970-01-01
          • 1970-01-01
          • 2021-03-18
          相关资源
          最近更新 更多