表视图单元格图像中的ios故障

表视图单元格图像中的ios故障,ios,swift,cocoa-touch,Ios,Swift,Cocoa Touch,我不熟悉swift语言。。。 使用图像制作简单表格,并希望在单元格内设置图像视图的角半径 代码:- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)! cell.text

我不熟悉swift语言。。。 使用图像制作简单表格,并希望在单元格内设置图像视图的角半径

代码:-

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)!

    cell.textLabel?.text = "Test"
    cell.detailTextLabel?.text = "\(indexPath.row)"

    if(cell.imageView != nil){
        cell.imageView?.image = UIImage(named: "avatar")
        cell.imageView?.layer.cornerRadius =  cell.imageView!.frame.size.height/2
        cell.imageView?.layer.masksToBounds = true

    }


    return cell
}
不知道我做错了什么,但当表格显示时角半径没有设置,当我开始滚动表格时,它设置正确

编辑:- 使用cell.imageView?.layer.cornerRadius=20获得良好结果
不知道为什么它使用静态值。

如果要创建自己的imageView,最好在自定义TableViewCell中设置cornerRadius

class CircularTableViewCell: UITableViewCell {
@IBOutlet weak var circularImageView: UIImageView!
override func layoutSubviews() {
    circularImageView.layer.cornerRadius = circularImageView.bounds.height / 2
    circularImageView.clipsToBounds = true
}

注意:除非将imageView的宽度和高度比设置为1:1,否则cornerRadius属性不能保证视图是绝对圆形的。创建圆形视图的另一种方法是使用遮罩。

如果要创建自己的imageView,最好在自定义TableViewCell内设置cornerRadius

class CircularTableViewCell: UITableViewCell {
@IBOutlet weak var circularImageView: UIImageView!
override func layoutSubviews() {
    circularImageView.layer.cornerRadius = circularImageView.bounds.height / 2
    circularImageView.clipsToBounds = true
}

注意:除非将imageView的宽度和高度比设置为1:1,否则cornerRadius属性不能保证视图是绝对圆形的。创建圆形视图的另一种方法是使用遮罩。

这是因为在第一次加载tableView时,ImageView仍然没有获得宽度或高度值。 您必须在情节提要的单元格内传递imageView的宽度和高度

如果未传递宽度或高度,请在ViewDidDisplay方法中重新加载表格视图

您可以按照图像中的建议在单元格中为ImageView设置约束

输出:

创建单元时应用角半径

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

static NSString *cellIdentifier = @"cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

UIImageView *imgView = (UIImageView *) [cell.contentView viewWithTag:9];

NSLog(@"Frame : %@", NSStringFromCGRect(imgView.frame));

imgView.layer.cornerRadius = imgView.frame.size.width/2.0;
imgView.layer.masksToBounds = YES;
//imgView.clipsToBounds = YES;

UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:99];
if(indexPath.row % 2 == 0){

    lbl.text = [NSString stringWithFormat:@"Index : %ld -- I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....",(long)indexPath.row];

}else{

    lbl.text = [NSString stringWithFormat:@"Index : %ld -- I am Odd....I am Odd....I am Odd....I am Odd....I am Odd....I am Odd....I am Odd....I am Odd....",(long)indexPath.row];

}
return cell;
}

发生这种情况的原因是,在首次加载tableView时,ImageView仍无法获取宽度或高度值。 您必须在情节提要的单元格内传递imageView的宽度和高度

如果未传递宽度或高度,请在ViewDidDisplay方法中重新加载表格视图

您可以按照图像中的建议在单元格中为ImageView设置约束

输出:

创建单元时应用角半径

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

static NSString *cellIdentifier = @"cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

UIImageView *imgView = (UIImageView *) [cell.contentView viewWithTag:9];

NSLog(@"Frame : %@", NSStringFromCGRect(imgView.frame));

imgView.layer.cornerRadius = imgView.frame.size.width/2.0;
imgView.layer.masksToBounds = YES;
//imgView.clipsToBounds = YES;

UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:99];
if(indexPath.row % 2 == 0){

    lbl.text = [NSString stringWithFormat:@"Index : %ld -- I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....I am Even....",(long)indexPath.row];

}else{

    lbl.text = [NSString stringWithFormat:@"Index : %ld -- I am Odd....I am Odd....I am Odd....I am Odd....I am Odd....I am Odd....I am Odd....I am Odd....",(long)indexPath.row];

}
return cell;
}

您的代码为true,但代码排序错误

这是:

cell.imageView?.layer.cornerRadius =  cell.imageView!.frame.size.height/2
cell.imageView?.layer.masksToBounds = true
cell.imageView?.image = UIImage(named: "avatar")

您的代码为true,但代码排序错误

这是:

cell.imageView?.layer.cornerRadius =  cell.imageView!.frame.size.height/2
cell.imageView?.layer.masksToBounds = true
cell.imageView?.image = UIImage(named: "avatar")


您使用的是AutoLayout吗?@technerd,是的,我使用的是..ImageView大小是固定的或取决于纵横比?我只设置了表格视图的约束,而不是单元格或单元格。ContentView您使用的是AutoLayout吗?@technerd,是的,我正在使用它。ImageView大小是固定的或取决于纵横比?我只设置了表视图的约束,而不是单元格或单元格。ContentView请检查编辑的帖子,它使用静态值。。。你对它有任何想法…请检查编辑后的文章,它与静态值工作。。。你对此有什么想法吗?谢谢@technerd,它在ViewDidDisplay中工作,但它在几毫秒后显示出来,我试图输入WillDisplay,但不幸的是它不工作。如果我不想首先向用户显示,还有其他解决方案吗?如何在单元格中为默认图像视图设置imageView的宽度和高度,而不是通过添加自定义图像视图。请查找已编辑的答案以设置imageView的约束。我在单元格中找不到图像。。。我们可以添加它,但我认为我们应该定制它。是否仍要在单元格的默认图像视图中添加此约束。如图中所示,只需将UIImageView放入单元格,并按图像中突出显示的方式应用约束。感谢@technerd,它在ViewDidDisplay中工作,但在几毫秒后显示,我尝试将WillDisplay放入,但不幸的是,它无法工作。如果我不想首先向用户显示,还有其他解决方案吗?如何在单元格中为默认图像视图设置imageView的宽度和高度,而不是通过添加自定义图像视图。请查找已编辑的答案以设置imageView的约束。我在单元格中找不到图像。。。我们可以添加它,但我认为我们应该定制它。是否仍要在单元格的默认图像视图中添加此约束。如图中所示,只需将UIImageView放入单元格中,并应用图像中突出显示的约束。