views:

273

answers:

3

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 see 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.

alt text

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;

}
A: 

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.

TechZen
I added the label to the content view, which fixed another little bug that I had, but the degration issue still exists.
Nic Hubbard
Also tried checking to see if the cornerRadius equals 10, but that still did not help.
Nic Hubbard
+2  A: 

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.

nan
+5  A: 

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]]];
drawnonward
+1 Heh, didn't notice.
TechZen
I didn't use the else statement, or the tag. I just moved my label code into the if statement, and it fixed my problem. Would there be any issues with how I did that?
Nic Hubbard
Ok, the radius is not degrading, but now my count won't update when I go back to that view...
Nic Hubbard
Without the else and the tag, count will be nil on future calls and the text will never be set.
drawnonward
But am I creating the UILable outside of the if/else? And where do I release?
Nic Hubbard