iOS UITableViewCell cell.imageView设置圆角

Sat*_*ran 9 uitableview uiimageview ios

嘿所有我试图设置cell.imageViewcornerRadius,但似乎没有用.

cell.imageView.layer.cornerRadius=9;
Run Code Online (Sandbox Code Playgroud)

它会起作用还是应该UIImageView在我的单元格中添加一个自定义圆角?

我也尝试过这个

cell.imageView.layer.borderWidth=2;
cell.imageView.layer.borderColor=[[UIColor blackColor]CGColor];
Run Code Online (Sandbox Code Playgroud)

但它似乎也没有用.有人遇到过类似的问题吗?

Nav*_*dse 28

首先将Framework - > QuartzCore.framework 添加到项目中,
然后在ViewController.m文件中导入头文件

#import <QuartzCore/CALayer.h>
Run Code Online (Sandbox Code Playgroud)

cellForRowAtIndexPath方法中添加以下代码:

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

    NSString *cellIdentifier = @"TableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

        // Rounded Rect for cell image
        CALayer *cellImageLayer = cell.imageView.layer;
        [cellImageLayer setCornerRadius:9];
        [cellImageLayer setMasksToBounds:YES];
    }                                

    cell.imageView.image = [UIImage imageNamed:@"image_name.png"];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,我刚刚错过了setMasksToBounds.犯了一个可怕的错误. (3认同)