UITableViewCell的子视图不能轮回

问题描述:

我想创建一个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

您将尝试使用创建一个自定义单元类,并创建您的视图不是以编程方式创建使用故事板。并在自定义单元格类中设置视图的属性。然后实现上面的代码,但稍微改变一下,我已经提到它。

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。请告诉我,如果你知道它。 –

这里面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://*.com/questions/12301256/is-it-possible-to -set-uiview-border-properties-from-interface-builder –

+0

感谢您的帮助。我已经用Rock的方式解决了这个问题。只使用自定义圆视图可以解决这个问题。但我不知道他们有什么不同。@ Kartik Singh Bhadoriya –

的rasterizationScale和shouldRasterize是“关闭屏幕渲染”,但它的全方位视角无影响。并且“view.clipsToBounds = YES”等于“view.layer.masksToBounds = 1”。我已经使用CGContextRef和UIBezierPath在UITableViewCell上创建了一个圆形视图,但它们不能创建真正的圆形视图。