ios - UITableView 中的重复文本

标签 ios xcode uitableview

我有一个带有表格 View 的 iOS 应用程序。当我多次选择行时,所选行中的信息重复。

你可以在图片上看到它: enter image description here

我以这种方式添加单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    UILabel *label = [[UILabel alloc] init];
    Place *item = [source objectAtIndex:indexPath.row];
    [label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]];
    label.text =item.name;
    label.frame = CGRectMake(5, 5, 310, 35);
    [cell addSubview:label];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    OnePlaceViewController *placeView = [[OnePlaceViewController alloc] initWithNibName:@"OnePlaceViewController" bundle:nil];
    placeView.nom = indexPath.row;
    placeView.source = source;
    placeView.route = route;
    placeView.titleView = titleView;
    /*
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    */
    [self.navigationController pushViewController:placeView animated:YES];
}

为什么信息会重复?谢谢。

最佳答案

你每次都在添加标签.. 像那样改变它:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    // Configure the cell...
    UILabel *label = [[UILabel alloc] init];
    Place *item = [source objectAtIndex:indexPath.row];
    [label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]];
    label.frame = CGRectMake(5, 5, 310, 35);
    label.tag = 666;
    [cell addSubview:label];
    // add this unless you're using ARC
    // [label release];
    // [cell autorelease];
  }

  UILabel* cellLabel = (UILabel*)[cell viewWithTag: 666];
  cellLabel.text = item.name;

  return cell;
}

关于ios - UITableView 中的重复文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9872042/

相关文章:

ios - shadowRadius 不会模糊阴影

ios - 未调用 BEMCheckBox 委托(delegate)

iphone - NSUserDefaults 无法保存 NSMutableDictionary

ios - 在 Swift 中跨多个目标共享 Storyboard

swift - 如何用我的 tableView 填充 View 的顶部?

ios - Appcelerator/Titanium - 在 iPhone X 上,如何使工具栏颜色填充到屏幕底部?

objective-c - UINavigationBar 不旋转

iphone - iPhone:更改XCode项目名称安全吗?

ios - 如何根据在 UITextView 中输入的文本更新 UITableViewCell 高度

ios - 具有多个 subview 问题的容器 View