2012-01-29 2 views
1

각 섹션의 키가 포함 된 3 개의 NSArrays에서 가져온 3 개의 섹션으로 UITableView를 설정했습니다. 각 셀에 대한 detailTextLabels 및 액세서리보기를 수동으로 설정하기 위해 switch 문을 설정하려고했지만 표보기를 스크롤 할 때마다 셀이 겉으로보기에는 바뀌 었습니다. 이 작업을 수행하는보다 효율적인 방법이 있습니까?DetailTextLabel은 UITableViewCell에서 계속 변경됩니다.

이것은 내가 사용하려고 한 방법이다 :

switch (indexPath.section) 
{ 
    // info 
    case 0:{ 
     cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row]; 
     if ([cell.textLabel.text isEqualToString:@"Duration"]) 
     { 
      cell.detailTextLabel.text = [task stringForDuration]; 
     } 
     if ([cell.textLabel.text isEqualToString:@"Elapsed"]) 
     { 
      // elapsed time 
     } 
     if ([cell.textLabel.text isEqualToString:@"Today"]) 
     { 
      UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
      [todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
      [todaySwitch setOn:task.isToday]; 
      cell.accessoryView = todaySwitch; 
      [todaySwitch release]; 
     } 
    } 
     break; 
    // actions 
    case 1:{ 
     cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row]; 
     if ([cell.textLabel.text isEqualToString:@"Priority"]) 
     { 
      // priority 
      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     } 
     if ([cell.textLabel.text isEqualToString:@"Finish Early"]) 
     { 
      // finish early 
     } 
     if ([cell.textLabel.text isEqualToString:@"Set New Time"]) 
     { 
      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     } 
     if ([cell.textLabel.text isEqualToString:@"Repeating"]) 
     { 
      UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
      [repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
      [repeatingSwitch setOn:task.isRepeating]; 
      cell.accessoryView = repeatingSwitch; 
      [repeatingSwitch release]; 
      //cell.detailTextLabel.text = nil; 
     } 
    } 
     break; 
    // details 
    case 2:{ 
     cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row]; 
     if ([cell.textLabel.text isEqualToString:@"Specifics"]) 
     { 
      cell.detailTextLabel.text = task.specifics; 
      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     } 
     if ([cell.textLabel.text isEqualToString:@"Repeated"]) 
     { 
      // times repeated 
     } 
    } 
     break; 
    default: 
     break; 
} 

가 그리고 이것은 또 다른입니다 :

// Info section 
if ([indexPath section] == 0) 
{ 
    // duration 
    cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row]; 
    if (indexPath.row == 0) 
    { 
     cell.detailTextLabel.text = [task stringForDuration]; 
    } 
    // elapsed time 
    if (indexPath.row == 1) 
    { 
     //cell.detailTextLabel.text = [task stringForTotalElapsedTime]; 
    } 
    // today status 
    if (indexPath.row == 2) 
    { 
     UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     [todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
     [todaySwitch setOn:task.isToday]; 
     cell.accessoryView = todaySwitch; 
     [todaySwitch release]; 

    } 
} 
// actions section 
if ([indexPath section] == 1) 
{ 
    cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row]; 
    // priority 
    if (indexPath.row == 0) 
    { 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    // finish early 
    if (indexPath.row == 1) 
    { 
     //[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    // set new time 
    if (indexPath.row == 2) 
    { 
     //cell.accessoryView = repeatingSwitch; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    // repeating 
    if (indexPath.row == 3) 
    { 
     UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     [repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
     [repeatingSwitch setOn:task.isRepeating]; 
     cell.accessoryView = repeatingSwitch; 
     [repeatingSwitch release]; 
     cell.detailTextLabel.text = nil; 
    } 
} 
// details section 
if ([indexPath section] == 2) 
{ 
    cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row]; 
    // specifics 
    if (indexPath.row == 0) 
    { 
     //cell.detailTextLabel.text = taskDetails.specifics; 
     cell.detailTextLabel.text = task.specifics; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    // repeated 
    if (indexPath.row == 1) 
    { 
     [cell setAccessoryView:nil]; 
    } 
} 

이러한 시도는 모두 cellForRowAtIndexPath

+0

정말 관련성이 높은 코드를 인용해야합니다. – Till

+0

무슨 일이 일어나는지는 스크롤을 많이하면, 일부 셀의 액세서리보기와 다른 사람의 세부 텍스트 레이블이 모든 행에 표시되기 시작합니다. [indexPath section]은 전체 섹션을 나타내는 것이 아니라 표시된 섹션을 나타내는 것으로 보입니다. 적어도 그것이 저에게 의미있는 유일한 것입니다. – gurooj

+0

세포가 재사용된다는 것을 알고 계시므로 이전에 유효한 설정이 계속 활성화되어 있습니까? 셀을 초기화 할 때 표시하거나 숨겨야하는 모든 것을 완전히 재설정해야합니다. – Till

답변

0

나 자신이 문제를 해결 결국 내부에 있었다, @ Till의 의견에 전적으로 감사드립니다. 이미 존재하는 셀을 재사용하는 것이므로 테이블에 여러 셀 유형을 사용하려면 다른 재사용 식별자를 사용해야합니다.

[cell setAccessoryType:UITableViewCellAccessoryNone]; 

switch ([indexPath section]) 
{ 
    case 0: 
    { 
     cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row]; 
     switch (indexPath.row) { 
      case 0: 
      { 
       cell.detailTextLabel.text = task.stringForDuration; 
       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 1: 
      { 
       cell.detailTextLabel.text = @"00:00"; 
       [cell setAccessoryType:UITableViewCellAccessoryNone]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 2: 
      { 
       UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
       [todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
       [todaySwitch setOn:task.isToday]; 
       cell.accessoryView = todaySwitch; 
       [todaySwitch release]; 
       [cell setAccessoryType:UITableViewCellAccessoryNone]; 
       cell.detailTextLabel.text = @""; 
      } 
       break; 
      default: 
       break; 
     } 
    } 
     break; 
    case 1: 
    { 
     cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row]; 
     switch (indexPath.row) { 
      case 0: 
      { 
       cell.detailTextLabel.text = @"Low"; 
       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 1: 
      { 
       cell.detailTextLabel.text = @""; 
       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 2: 
      { 
       UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
       [repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
       [repeatingSwitch setOn:task.isRepeating]; 
       cell.accessoryView = repeatingSwitch; 
       [repeatingSwitch release]; 
       cell.detailTextLabel.text = @""; 
       [cell setAccessoryType:UITableViewCellAccessoryNone]; 
      } 
       break; 
      default: 
       break; 
     } 
    } 
     break; 
    case 2: 
    { 
     cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row]; 
     switch (indexPath.row) { 
      case 0: 
      { 
       cell.detailTextLabel.text = task.specifics; 
       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 1: 
      { 
       cell.detailTextLabel.text = @"0"; 
       [cell setAccessoryType:UITableViewCellAccessoryNone]; 
       cell.accessoryView = nil; 
      } 
       break; 
      default: 
       break; 
     } 
     break; 
    } 
    default: 
     break; 
} 

이 방법은 나에게 문제를 저장 : 만 7 개 또는 8 테이블 뷰 셀이 있기 때문에 아니면, 난 그냥하는 UISwitch가 포함되지 할 그래서 같은 모든 세포 전무로 셀 액세서리보기를 설정 다중 재사용 식별자 다루기.

관련 문제