【问题标题】:How to set corner radius for TableView cells in ios如何在ios中为TableView单元格设置角半径
【发布时间】:2016-06-14 22:37:04
【问题描述】:

嗨,我是 iOS 新手,在我的应用程序中我使用的是 TableList,我想将 tableList 单元格角半径设置为 3.0。这是我的代码。它仅在左角而不是所有角上应用圆角半径。

我的代码:-

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 75;
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *simpleTableIdentifier = @"MyCell";

        Cell = (MyTripsCell *)[MaintableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (Cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTripsCell" owner:self options:nil];
            Cell = [nib objectAtIndex:0];
        }

        Cell.layer.cornerRadius = 0.0;
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return Cell;
    }


    -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath{

        cell.contentView.backgroundColor = [Bg colorWithHexString:@"EEEEEE"];
        UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 12, MaintableView.frame.size.width, 70)];

        whiteRoundedView.layer.backgroundColor = [Bg colorWithHexString:@"FDFDFD"].CGColor;

        whiteRoundedView.layer.masksToBounds = YES;
        whiteRoundedView.layer.cornerRadius = 3.0;
        whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, -1);
        [cell.contentView addSubview:whiteRoundedView];
        [cell.contentView sendSubviewToBack:whiteRoundedView];

        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            [cell setPreservesSuperviewLayoutMargins:NO];
        }
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }

【问题讨论】:

  • 你在使用自动布局吗?
  • 不,我没有使用自动布局
  • 两个左角你得到半径了吗?顶部还是底部?
  • 在 cellforrowat indexpath 中调用你的函数,它确实有效
  • 我要设置圆角半径的所有边

标签: ios objective-c uitableview


【解决方案1】:

问题

代码没问题,因为它在每个角落都做圆角半径,但问题是您实际上将视图的原点移动到 y=12。该视图与 tableView 具有相同的宽度,并且它具有角半径,当视图在右侧超出屏幕时,您只是看不到这一点。

解决方案

为了让您的视图位于屏幕中间,您将其中心设置为 contentView 的中心。并使用宽度来设置填充。

您的代码解决方案:

// Using this width and setting the view to the center will give you padding of 12 pixels on left and right
NSInteger width = MaintableView.frame.size.width - 24;
UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 70)];
whiteRoundedView.center = cell.contentView.center;

whiteRoundedView.layer.backgroundColor = [Bg colorWithHexString:@"FDFDFD"].CGColor;

whiteRoundedView.layer.masksToBounds = YES;
whiteRoundedView.layer.cornerRadius = 3.0;
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, -1);
[cell.contentView addSubview:whiteRoundedView];
[cell.contentView sendSubviewToBack:whiteRoundedView];

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
    [cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
    [cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
}

无论如何,我可以通过在每次出列单元格时不添加该视图来对该代码进行一些改进。另请注意,为此视图设置maskToBounds = YES,将导致您的阴影不显示(请参阅here)

我的代码解决方案(已编辑)

UIView *roundedView = [cell.contentView viewWithTag:100];
if (roundedView == nil) {
    // Add some padding to the view in order to see the shadow
    NSInteger spacingBothHorizontal = 2;
    CGRect customizedFrame = CGRectMake(spacingBothHorizontal/2, 0, CGRectGetWidth(cell.contentView.frame) - spacingBothHorizontal, CGRectGetHeight(cell.contentView.frame) - 20);
    roundedView = [[UIView alloc] initWithFrame:customizedFrame];
    
    // Layer customizations
    roundedView.layer.cornerRadius = 10.0f;
    roundedView.backgroundColor = [UIColor redColor];
    roundedView.layer.shadowOffset = CGSizeMake(-1, -1);
    roundedView.layer.shadowOpacity = 2.0;
    roundedView.layer.shadowColor = [UIColor blueColor].CGColor;
    roundedView.tag = 100;
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    // Add it to view
    [cell.contentView addSubview:roundedView];
    [cell.contentView sendSubviewToBack:roundedView];
    cell.contentView.backgroundColor = [UIColor blackColor];
}

结果

【讨论】:

  • 嗨,我想为单元格空间应用颜色
  • 您可以为将驻留在每个单元格底部的视图添加一个视图。而且你可以只在原点水平和垂直设置roundedView的中心,所以它会创建一个单元格空间的想法
  • 我对这项技术非常陌生,请提供一些代码
  • @AbhiRam 我编辑了答案以符合您的要求,但是由于您是新来的,所以我为您做了,但是您必须自己尝试一下,如果没有成功,请提出另一个问题。希望这会有所帮助,您可以投票并接受它。
  • 好的,但是除了剩余的单元格之外,所有颜色都必须是黑色的,但你的屏幕似乎不是那样的,我希望你能理解
【解决方案2】:

这是示例代码。 这是您要找的吗?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    if ([cell viewWithTag:100] == nil)
    {
        UIView* roundedView = [[UIView alloc]initWithFrame:CGRectInset(cell.bounds, 2, 2)];
        roundedView.backgroundColor = [UIColor lightGrayColor];
        roundedView.tag = 100;
        roundedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell insertSubview:roundedView atIndex:0];
        roundedView.layer.cornerRadius = 5;
        roundedView.layer.masksToBounds = true;
    }

    return cell;
}

在你的情况下,使用以下并删除 willdisplaycell

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

    }
    if ([cell viewWithTag:100] == nil)
    {
        UIView* roundedView = [[UIView alloc]initWithFrame:CGRectInset(cell.bounds, 2, 2)];
        roundedView.backgroundColor = [UIColor lightGrayColor];
        roundedView.tag = 100;
        roundedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell insertSubview:roundedView atIndex:0];
        roundedView.layer.cornerRadius = 5;
        roundedView.layer.masksToBounds = true;
    }
    cell.textLabel.text = @"Hello world";

    return cell;
}

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 75;
}

【讨论】:

  • willdisplay委托方法呢?
  • 为什么需要那个方法?在单独的项目中尝试这个示例代码,看看 id 这就是你要找的。我认为这里不需要 willdisplay 方法。
  • 这是我的示例项目,我想为每个单元格设置黑白空间并想设置角半径,请查看一次
  • 你看到我的项目文件了吗?
【解决方案3】:

可能的原因是:

你的单元格高度是 75。你正在写这个:

UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 12, MaintableView.frame.size.width, 70)];

查看实际CGRectMake

CGRect CGRectMake ( CGFloat x, CGFloat y, CGFloat width, CGFloat height );

在您的情况下,y = 12。根据单元格的坐标,它是超级视图的 (0,0,width,height),当您放置 (0, 12, width, height) 的子视图(您的 whiteRoundedView )时,其下部将不会显示。作为其超出单元格的视口。试着理解这些坐标系。

编辑:

将您的视图代码更改为:

UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 3, cell.frame.size.width, 69)];

并将圆角半径设置为 10。

whiteRoundedView.layer.cornerRadius = 10.0;

运行应用程序并查看差异。

【讨论】:

  • 有什么办法解决这个问题?
猜你喜欢
  • 2016-03-31
  • 2021-03-02
  • 1970-01-01
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多