【问题标题】:Square instead of rounded corners in UITableViewCell grouped styleUITableViewCell 分组样式中的方形而不是圆角
【发布时间】:2011-12-03 10:52:46
【问题描述】:

我希望我的分组表格视图单元格使用方角而不是默认的圆角,而且我不只是想使用图像来产生这种效果。有可能吗?

【问题讨论】:

  • 为什么不自定义一个 UITableviewStylePlain?..

标签: iphone objective-c uitableview cell tableview


【解决方案1】:

最简单的,在你的tableView:cellForRowAtIndexPath: 使用

cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease];

【讨论】:

  • 在 ARC 领域省略自动释放调用。
【解决方案2】:

您可以将 UITableViewCell 的 backgroundViewselectedBackgroundView 设置为您自己创建的自定义 UIView。这应该会给你一个方形单元格。

【讨论】:

    【解决方案3】:

    接受的答案效果很好,但不幸的是删除了单元格之间的分隔线。如果你有一个 3x3 像素的 TableCellBackground.png,前两行像素为白色,最低的第三行为灰色(以匹配分隔符颜色),你可以这样做:

        // To square the corners, we replace the background view of the top and bottom cells.
        // In addition, the top cell needs a separator, which we get from TableCellBackground.png.
        UIImage *stretchableImage = [UIImage imageNamed:@"TableCellBackground.png"];
        UIImage *cellImage = [stretchableImage resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds];
        imageView.image = cellImage;
        cell.backgroundView = imageView;
    

    【讨论】:

      【解决方案4】:

      首先根据需要设置单元格的背景视图,然后设置单元格的选定背景视图(与单元格背景的边界相同)

      你可以指定cornerRadius属性,这样你就可以根据需要设置圆角,我在我的例子中省略了这个属性

      这是两者的代码:

              UIView *bg = [[UIView alloc] initWithFrame:cell.bounds];
              bg.backgroundColor = [UIColor colorWithRed:0.980 green:0.988 blue:0.984 alpha:1];
              bg.layer.borderColor = [UIColor colorWithRed:0.827 green:0.827 blue:0.835 alpha:1].CGColor;
              bg.layer.borderWidth = kCellBorderWidth;
      //        bg.layer.cornerRadius= kCellBorderRadius;
              cell.backgroundView = bg;
      
              // to make cell selection square and not round (which is by default)
              UIView *bg_selected = [[UIView alloc] initWithFrame:cell.bounds];
              bg_selected.backgroundColor = [UIColor lightGrayColor];
              bg_selected.layer.borderColor = [UIColor colorWithRed:0.827 green:0.827 blue:0.835 alpha:1].CGColor;
              bg_selected.layer.borderWidth = kCellBorderWidth;
              cell.selectedBackgroundView = bg_selected;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-02
        • 1970-01-01
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        相关资源
        最近更新 更多