【问题标题】:iPhone: cornerRadius quality gets degradediPhone:cornerRadius 质量下降
【发布时间】:2010-06-09 20:21:18
【问题描述】:

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

一开始看起来不错,但是一旦您深入该行并返回几次,圆边的质量就会开始下降。您可以在屏幕截图中看到,左侧是块状的。

为什么会发生这种情况?它似乎在加载该视图大约 2 次后发生。


(来源:puc.edu

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

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

}

【问题讨论】:

  • 你能发布更多你的细胞创建方法吗?
  • 没问题,我已经更新了代码。

标签: iphone uitableview uilabel


【解决方案1】:

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

仅在 if (cell == nil) 块中创建新单元格时尝试创建 count 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]]];

【讨论】:

  • 我没有使用 else 语句或标签。我只是将我的标签代码移动到 if 语句中,它解决了我的问题。我这样做会有什么问题吗?
  • 好的,半径没有降低,但是当我返回那个视图时,我的计数不会更新...
  • 如果没有 else 和标签,count 将在以后的调用中为零,并且永远不会设置文本。
  • 但是我是在 if/else 之外创建 UILable 吗?我在哪里发布?
  • 标签也应该是一个 NSInteger。
【解决方案2】:

检查表格视图/单元格是否设置为在绘制之前清除其上下文。我注意到单元格上的文本有类似的问题。

【讨论】:

    【解决方案3】:

    我看到了一些奇怪的问题,其中看起来基于 Core Animation 的属性是累积的,即使它们不应该累积。您的问题可能是由于每次返回单元格时重复更改角半径的值而导致的某种累积蠕变。

    我建议在重新设置之前测试拐角半径是否已经等于 10。 (虽然我希望上下滚动比重新加载视图时显示更多。)

    另一个可能的问题是某些子视图正在迁移导致视觉伪影。

    编辑:

    而不是将标签添加到单元格本身。尝试将其添加为单元格内容视图的子视图。

    【讨论】:

    • 我在内容视图中添加了标签,这修复了我遇到的另一个小错误,但降级问题仍然存在。
    • 还尝试检查cornerRadius是否等于10,但这仍然没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多