2014-05-11 70 views
0

假设你有一个自定义的UITableViewCell,并在你有一个UIView的单元格内,你想要一个右下角和右上角的半径。哪里才是做出这些改变的正确地点?在UITableViewCell中修改UIView外观的正确方法是什么?

我假设它不在UITableViewCell的drawRect内,因为这会是一个巨大的性能影响。 UITableViewCell内部还有其他一些功能吗?或者,我应该在我的UITableViewController在函数内部做这个

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

回答

1

一旦你的cellForRowAtIndexPath中实例化您的自定义单元格我会做到这一点:

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

NSString *cellIdentifier = @"cell"; 
UICustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.customview.layer.cornerRadius = 5.0f; 
    //add other custom stuff here as well 
} 

return cell; 
} 
+0

你能解释一下为什么这会在性能上是最好的? –

+0

如果单元格不存在,则对其执行自定义修改 - 对于每个**被检测的新单元格,这只会发生一次** - 而不是在if语句之外执行,这将导致自定义更改将被执行多次,也就是每个正在显示的单元格。 – royherma

+0

那么既然你在重复使用单元格,它只会在第一次创建时调用这些更改? –

0

由于这一观点被细胞所拥有的,我会在单元格的init方法中完成它 - 我认为最好保留任何视图设置或修​​改,这些设置或修改不依赖于表视图的数据源方法中的索引路径(即不是动态的)。哪个init方法取决于你如何制作你的单元格。如果单元格是在故事板中制作的,则使用initWithCoder :,如果在xib中,则使用initWithNibName:bundle :.另一种方法是将该视图本身进行子类化,并在其init方法中进行修改。

0

我建议使用UITableViewCell子类,并将自定义视图的自定义视图添加到contentView中。

这是我的简单实现。

#import "TableViewCell.h" 
#import "RoundedView.h" 

@implementation TableViewCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     RoundedView *roundedView = [[RoundedView alloc] initWithFrame:CGRectInset(self.contentView.bounds, 2, 2)]; 
     [self.contentView addSubview:roundedView]; 
    } 
    return self; 
} 

@end 

而圆形我简单地设置圆角到所需的边缘。

#import "RoundedView.h" 

@implementation RoundedView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 


- (void)drawRect:(CGRect)rect{ 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    UIBezierPath *bp = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)]; 
    CGContextAddPath(context, bp.CGPath); 
    CGContextClip(context); 
    [[UIColor brownColor] setFill]; 
    UIRectFill(rect); 

} 

@end 

而且里面的cellForRowAtIndexPath :,

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; 
    if(!cell){ 
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; 
    } 
    return cell; 
} 

注意,因为该视图被添加到细胞里面只有initWithStyle:reuseIdentifier:方法,认为只会当一个新的小区分配借鉴。大多数情况下,UITableView会使单元出队,所以这只是很少的时间。

而最终的结果是这样的,

enter image description here

+0

我相信绘制drawRect函数中的单元格不利于性能,并且会减慢滚动效果。 –

+0

为什么不能。所有的UIView类都在drawRect:方法中自己绘制。然而,适当的缓存和避免重绘会大大提高性能。 – Sandeep

相关问题