2016-08-25 80 views
1

我想創建一個tableview,它具有一些圓形子視圖作爲UIView。我設置了UIView的layer.cornerRadius和clipsToBounds。但是有些觀點並不全面。誰可以幫我解決這個問題或給我一些建議?UITableViewCell的子視圖不能輪迴

我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString * settCellID = @"settingCellID"; 
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:settCellID]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settCellID]; 

     UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)]; 
     view.layer.cornerRadius = 10; 
     view.clipsToBounds = 1; 
     view.layer.rasterizationScale = [UIScreen mainScreen].scale; 
     view.layer.shouldRasterize = 1; 
     view.backgroundColor = [UIColor blueColor]; 
     [cell.contentView addSubview:view]; 
    } 
    return cell; 
} 

結果:

enter image description here

+0

這是發生在設備上還是在模擬器上? – beyowulf

+0

作爲行高返回的值是多少。比這個圓形視圖的高度大。 –

+0

這是發生在模擬器?那麼就有模擬器毛刺的可能性。將模擬器的比例設置爲100%(在菜單:窗口 - >比例 - > 100%)然後檢查。 – Jeyamahesan

回答

1

您將嘗試使用創建一個自定義單元類,並創建您的視圖不是以編程方式創建使用故事板。並在自定義單元格類中設置視圖的屬性。然後實現上面的代碼,但稍微改變一下,我已經提到它。

static NSString * settCellID = @"settingCellID"; 
'CustomClassName' * cell = (CustomClassName*)[tableView dequeueReusableCellWithIdentifier:settCellID]; 
cell.viewPropertyName.layer.cornerRadius = 10; 
cell.viewPropertyName.clipsToBounds = YES; 
cell.viewPropertyName.maskToBounds =YES; 
cell.viewPropertyName.backgroundColor = [UIColor blueColor]; 
+0

感謝您的幫助,我解決了使用您的建議的問題。我不知道我們創建圓視圖的方式有什麼不同。使用你的方式的圓形視圖更像一個clrcle。請告訴我,如果你知道它。 –

0

這裏面clipsToBound是布爾所以你應該給YES或NO,並嘗試使用maskToBound = YES拐角半徑如果你想合併下面的視圖比使用clipsToBound相應,我不認爲這rasterizationScale和shouldRasterize在這裏很有用。我希望這能幫到您。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString * settCellID = @"settingCellID"; 
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:settCellID]; 
if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settCellID]; 

    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)]; 
    view.layer.cornerRadius = 10; 
    view.clipsToBounds = YES; 
    view.maskToBounds =YES; 
    view.layer.rasterizationScale = [UIScreen mainScreen].scale; 
    view.layer.shouldRasterize = 1; 
    view.backgroundColor = [UIColor blueColor]; 
    [cell.contentView addSubview:view]; 
} 
return cell; 
} 

如果你想要的UITableViewCell圓角和剪輯子視圖不是按照這個Link Click here

+0

rasterizeScale和shouldRasterize是用於「離屏渲染」,但它對圓視圖沒有影響。並且「view.clipsToBounds = YES」等於「view.layer.masksToBounds = 1」。我已經使用CGContextRef和UIBezierPath在UITableViewCell上創建了一個圓形視圖,但它們不能創建真正的圓形視圖。 –

+0

然後在Storyboard中創建自定義單元格並設置用戶定義運行時屬性我確定這將幫助您使用用戶定義運行時屬性更喜歡此鏈接http://stackoverflow.com/questions/12301256/is-it-possible-to -set-uiview-border-properties-from-interface-builder –

+0

感謝您的幫助。我已經用Rock的方式解決了這個問題。只使用自定義圓視圖可以解決這個問題。但我不知道他們有什麼不同。@ Kartik Singh Bhadoriya –

0

的rasterizationScale和shouldRasterize是「關閉屏幕渲染」,但它的全方位視角無影響。並且「view.clipsToBounds = YES」等於「view.layer.masksToBounds = 1」。我已經使用CGContextRef和UIBezierPath在UITableViewCell上創建了一個圓形視圖,但它們不能創建真正的圓形視圖。

相關問題