iOS UITableViewCell cell.imageView设置圆角 [英] iOS UITableViewCell cell.imageView setting rounded corner

查看:299
本文介绍了iOS UITableViewCell cell.imageView设置圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有我试图设置 cell.imageView cornerRadius ,但它似乎没有工作。

Hey all I am trying to set cell.imageView's cornerRadius, but it doesn't seem to work.

cell.imageView.layer.cornerRadius=9;

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

Will it work or should I add a custom UIImageView in my cell to have rounded corners?

我也试过这个

cell.imageView.layer.borderWidth=2;
cell.imageView.layer.borderColor=[[UIColor blackColor]CGColor];

但它似乎也不起作用。有没有人遇到过类似的问题?

But it also doesn't seem to work. Has anybody faced a similar issue?

推荐答案

首先添加框架 - > QuartzCore.framework到您的项目

然后在您的 ViewController.m 文件中导入头文件

First add Framework -> QuartzCore.framework to your project
then import header file in your ViewController.m file

#import <QuartzCore/CALayer.h>

cellForRowAtIndexPath 方法中添加以下代码:

Add following code in your cellForRowAtIndexPath method:

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

这篇关于iOS UITableViewCell cell.imageView设置圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆