2015-12-31 142 views
0

我是新的swift语言... 制作带图片的简单表格,并希望在单元格内设置图像视图的角半径。ios在表格视图单元格图像中出现故障

代码: -

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 
} 

不知道我在做什么错,但是当我开始滚动表它正确设置圆角半径不seted当表显示... enter image description here

编辑: - 与cell.imageView?.layer.cornerRadius = 20 得到很好的结果不知道它为什么与静态值一起工作。

+0

是否使用自动版式? – technerd

+0

@technerd,是的,我正在使用它.. –

+0

ImageView的大小是固定的还是取决于长宽比? – technerd

回答

3

发生这种情况是由于ImageView在第一次加载tableView时仍未获取宽度或高度值。 您必须在Storyboard中的单元格内传递imageView的宽度和高度。

如果您没有传递宽度或高度,然后在viewDidAppear方法中重新加载表视图。

如图所示,您可以在Cell中为ImageView设置约束。

enter image description here

enter image description here

输出:

enter image description here

创建单元格时应用圆角半径。

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

感谢@technerd,它在viewDidAppear中工作,但它显示几毫秒后,我试图把willAppear,但不幸的是它不工作。 任何其他解决方案,如果我不想首先显示给用户。 –

+0

如何设置默认图像视图的单元格中的imageView宽度高度,而不是通过添加自定义图像视图 –

+0

请查找编辑的答案为ImageView设置约束。 – technerd

0

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

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

注意cornerRadius属性不能保证该视图将是绝对圆的,除非你设置的ImageView宽度和高度的比例为1:1。另一种创建圆视图的方法是使用Mask。

+0

相同的结果@piyush patel .... –

+0

请检查编辑的文章,它的工作原理与静态值...你有任何想法... ... –

0

你的代码是真实的,但代码排序错误,这是

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