UITableView中的重复文本

时间:2012-03-26 12:23:39

标签: ios xcode uitableview

我有一个带有桌面视图的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];
}

为什么信息会重复?日Thnx。

2 个答案:

答案 0 :(得分:8)

你要添加一个标签EVERYTIME ..改变它:

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

答案 1 :(得分:7)

我认为问题在于您每次都将UILabel添加为subView。由于单元格被重用,每次表格重新加载时,都会添加多个UILabel个。因此,我的建议是遍历单元格的所有子视图,并在添加新标签之前删除它们。

- (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);
for(UIView *v in [cell subviews])
{
    if([v isKindOfClass:[UILabel class]])
       [v removeFromSuperview];
}
[cell addSubview:label];
return cell;
}