質問

Say that you have a custom UITableViewCell and within the cell you have a UIView in which you wanted to have a bottom right and top right radius. Where would be the correct place to make these changes?

I'm assuming that it is not within the drawRect of the UITableViewCell because that would be a huge performance hit. Is there some other function within UITableViewCell to do this? Or should I be doing this in my UITableViewController within the function

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
役に立ちましたか?

解決

I would do it once you instantiate your custom cell within the 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;
}

他のヒント

Since this view is owned by the cell, I would do it in the cell's init method -- I think it's best to keep any view setup or modifications that doesn't depend on the index path (i.e. aren't dynamic) out of the table view's data source methods. Which init method depends on how you make your cell. If the cell is made in the storyboard, then use initWithCoder:, if in a xib, then initWithNibName:bundle:. The other alternative is to subclass that view itself, and do the modification in its init method.

I suggest to use the UITableViewCell subclass and add the custom view with the custom radius into the contentView.

Here is my simple implementation.

#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

And the rounded my simply set the rounded corner to the required edges.

#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

And inside the cellForRowAtIndexPath:,

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

Note since the view is added to cell only inside initWithStyle:reuseIdentifier: method, the view will only be draw when the a new cell is allocated. Most of the time, UITableView dequeues the cell and so this is only drawn few time.

And the final result looks like this,

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top