开发者

iPhone: cornerRadius quality gets degraded

I have a UILabel that I create a radius on the layer, using cornerRadius. The ultimate goal is to make the label look like Apple does in the mail app.

It looks great at first, but once you drill down into that row and back a few times, the quality of the rounded edge starts to degrade. You can se开发者_JAVA技巧e in the screen shot, the left side is blocky.

Why would this be happening? It seems to happen after about 2 times of loading that view.

iPhone: cornerRadius quality gets degraded

(source: puc.edu)

Here is my cell creation method:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    Trip *trip = [fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = trip.title;
    cell.detailTextLabel.text = @"Date of trip";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // Create a nice number, like mail uses
    UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 50, 12, 34, 20)];
    [count  setText:[NSString stringWithFormat:@"%i",[[trip.rides allObjects] count]]];
    [count setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    count.textAlignment = UITextAlignmentCenter;
    count.backgroundColor = [UIColor grayColor];
    count.textColor = [UIColor whiteColor];
    count.layer.cornerRadius = 10;
    [cell addSubview:count];
    [count release];

    return cell;

}


In every call to cellForRowAtIndexPath, you are creating a new count UILabel. Eventually there will be several overlapping views in the same place with the same antialiased curve, so it will look blocky.

Try creating the count UILabel only when a new cell is created, in the if (cell == nil) block. Otherwise, get the count label by tag.

if ( cell == nil ) {
  cell = ...
  count = ...
  ...
  count.tag = 'coun';
  [cell.contentView addSubview:count];
  [count release];
} else {
  count = (UILabel *)[cell viewWithTag:'coun'];
}

[count  setText:[NSString stringWithFormat:@"%i",[[trip.rides allObjects] count]]];


Check to see if the tableview/cell is set to clear its context before drawing. I noticed I had similar issues with text on the cell.


I've seen some strange issues wherein it looks like Core Animation based properties are accumulative even though they shouldn't be. Your problem here might be caused by some kind of accumulative creep from changing the value of the corner radius repeatedly every time the cell is returned.

I would suggest testing if the corner radius already equals 10 before setting it again. (Although I would expect that to show up more with scrolling up and down than in reloading the view.)

Another possible problem would be that some subview is migrating causing a visual artifact.

Edit:

Instead of adding the label to the cell itself. Try adding it as a subview of the cell's content view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜