Дублированный текст в UITableView - PullRequest
5 голосов
/ 26 марта 2012

Я получил приложение для 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 ]

8 голосов
/ 26 марта 2012

вы добавляете ярлык ВСЁ ... меняйте его следующим образом:

- (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;
}
7 голосов
/ 26 марта 2012

Я думаю, проблема в том, что вы добавляете UILabel в качестве subView каждый раз.Поскольку ячейки используются повторно, при каждой перезагрузке таблицы добавляется несколько UILabel s.Поэтому я предлагаю просмотреть все подпредставления ячейки и удалить их, прежде чем добавлять новый ярлык.

- (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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...