2015-12-31 47 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") 
相關問題