【问题标题】:iPhone Device 3.1 SDK Breaks vertical alignment of UITableViewCellStyleValue1 textLabeliPhone Device 3.1 SDK 打破 UITableViewCellStyleValue1 textLabel 的垂直对齐方式
【发布时间】:2009-09-10 22:43:03
【问题描述】:

谁能解释以下现象?

从 iPhone Device 3.1 SDK 开始,我发现如果 UITableViewCell 的样式为 UITableViewCellStyleValue1 并且其 detailTextLabel.text 未分配,则 textLabel 不会显示在单元格的中心应该会。

一个值得注意的警告是,这只发生在我在 Device 上进行测试时——iPhone Simulator 3.1 SDK 可以正确显示单元格。此外,在使用 iPhone Device 3.0 SDK 时,这不是问题。

下面是一个简单的UITableViewController 子类实现来演示这个问题。

@implementation BuggyTableViewController

#pragma mark Table view methods


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    switch (indexPath.row) {
  case 0:
   cell.textLabel.text = @"detailTextLabel.text unassigned";
   break;
  case 1:
   cell.textLabel.text = @"detailTextLabel.text = @\"\"";
   cell.detailTextLabel.text = @"";
   break;
  case 2:
   cell.textLabel.text = @"detailTextLabel.text = @\"A\"";
   cell.detailTextLabel.text = @"A";
   break;
  default:
   break;
 }

    return cell;
}

@end

【问题讨论】:

  • 这也是在 3.0 中,你应该在你知道会有一个 detailTextLabel 的单元格中设置具有该样式的分配单元格,如果没有,则设置为默认值。
  • 我在 3.1.2 中也遇到了这个。完全相同:仅在设备上。我还注意到单元格中的标签在视图显示后使用动画移动到其不正确的位置。我拍了一部这样的电影,只是为了向自己证明我不是在想象它。使用 cell.detailTextLabel.text = @""; 的解决方法为我修好了。

标签: iphone uitableview


【解决方案1】:

而不是

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

尝试使用

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。单元格在模拟器中看起来不错,但是当我在设备上运行应用程序时,单元格文本没有垂直居中。使用 UITableViewCellStyleDefault 解决了我的垂直对齐居中问题。幸好我的单元格没有字幕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多