iPhone:cornerRadius质量下降

Nic*_*ard 2 iphone uitableview uilabel

我有一个UILabel,我使用cornerRadius在图层上创建一个半径.最终目标是使标签看起来像Apple在邮件应用程序中所做的那样.

它起初看起来很棒,但是一旦你深入到那一行并向后钻了几次,圆边的质量就会开始下降.你可以在屏幕截图中看到,左侧是块状的.

为什么会这样?似乎在加载该视图约2次后发生.

alt text http://web2.puc.edu/PUC/files/iphone-number.png

这是我的单元格创建方法:

- (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;

}
Run Code Online (Sandbox Code Playgroud)

dra*_*ard 8

在每次调用cellForRowAtIndexPath时,您都在创建一个新的countUILabel.最终在同一个地方会有几个重叠的视图具有相同的抗锯齿曲线,所以它看起来很块.

尝试count仅在if (cell == nil)块中创建新单元时创建UILabel .否则,count按标签获取标签.

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]]];
Run Code Online (Sandbox Code Playgroud)