2012-11-22 54 views
1

在我的应用程序中,用户根据他/她选择的单元格更改显示在tableView中的字段。 (仅供参考...我允许多个单元格选择。)只要用户按下后退按钮,程序就会将选定单元格的textLabel复制到父级viewController的占位符。保持所选单元格的轨迹

这里是我的代码的相关章节:

- (void)willMoveToParentViewController:(UIViewController *)parent 
{ 
int tempCount = 0; 

for (int section = 0; section < 3; section++) 
{ 
    int rowMax; 

    if (section == 0) 
     rowMax = 3; 

    else if (section == 1) 
     rowMax = 5; 

    else if(section == 2) 
     rowMax = 3; 

    for(int row = 0; row < rowMax; row++) 
    { 
     NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; 

     UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:tempIndexPath]; 

     if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) 
     { 
      NSLog(@"tempCount = %d", tempCount); 
      tempCount++; 

      if (tempCount == 1) 
       chosenFieldsViewController.field0.placeholder = selectedCell.textLabel.text; 

      else if(tempCount == 2) 
       chosenFieldsViewController.field1.placeholder = selectedCell.textLabel.text; 

      else if(tempCount == 3) 
       chosenFieldsViewController.field2.placeholder = selectedCell.textLabel.text; 

     } 
    } 
} 
} 

我意识到,如果的tableView向下滚动选择细胞后,所选择的细胞不会出现在parentVC占位符。从我的分析中我认为,一旦我向下滚动,单元就从内存中删除。因此,尽管选择了这些单元格,但它们未能显示在父VC上。

如果是这样,当我向上滚动时,为什么看到单元格显示为选中状态?

如果有人能够建议我如何保持所选单元格的轨道,即使用户滚动,我将不胜感激。

谢谢。

编辑1

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

if(selectedCell.accessoryType != UITableViewCellAccessoryCheckmark && count<3) 
{ 
    selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    count ++; 
} 

else if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) 
{ 
    selectedCell.accessoryType = UITableViewCellAccessoryNone; 
    count--; 
} 

} 

编辑2

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

NSString *CellIdentifier = [NSString stringWithFormat:@"CellForRow%dSection%d",indexPath.row, indexPath.section]; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 

cell.selectionStyle = UITableViewCellSelectionStyleNone; // VERY IMPORTANT 

if (indexPath.section == 0) 
{   
    switch(indexPath.row) 
    { 
     case 0: 
      cell.textLabel.text = @"Class A"; 
      break; 

     case 1: 
      cell.textLabel.text = @"Class B"; 
      break; 

     case 2: 
      cell.textLabel.text = @"Class C"; 
      break; 

     default: 
      break; 
    } 
} 

if(indexPath.section == 1) 
{   
    switch(indexPath.row) 
    { 
     case 0: 
      cell.textLabel.text = @"Class D"; 
      break; 

     case 1: 
      cell.textLabel.text = @"Class E"; 
      break; 

     case 2: 
      cell.textLabel.text = @"Class F"; 
      break; 

     case 3: 
      cell.textLabel.text = @"Class G"; 
      break; 

     case 4: 
      cell.textLabel.text = @"Class H"; 
      break; 

     default: 
      break; 
    } 
} 

if(indexPath.section == 2) 
{ 
    switch (indexPath.row) 
    { 
     case 0: 
      cell.textLabel.text = @"Class I"; 
      break; 

     case 1: 
      cell.textLabel.text = @"Class J"; 
      break; 

     case 2: 
      cell.textLabel.text = @"Class K"; 
      break; 

     default: 
      break; 
    } 
} 

return cell; 
} 
+0

当用户输入文字时,您需要这样做'chosenFieldsViewController.field0.placeholder = selectedCell.textLabel.text;'。否则,您必须以某种形式将其存储在您的课堂中,以便在滚动时您不会丢失此信息。 – iDev

+0

@ACB:我曾想过你提到的完全相同的解决方案。但它有点棘手。有三个部分,我希望这些字段的显示顺序与他们已选择的顺序相同。如果用户取消选择一个字段并从另一个部分选择一个字段,代码会变得稍微复杂。为了帮助你形象化,请参考:http://stackoverflow.com/questions/13460535/label-text-getting-mixed-up。事实上,我仍然感谢你帮助我解决了应用程序的一个主要问题。 :-) –

+0

很高兴知道它有帮助。 :)你能分享一些更多的代码吗?实现上述目的的唯一方法是将其存储为当用户输入文本时。如果您可以分享更多的代码,那么我们可能会检查如何克服您面临的复杂情况。根据我的理解,当用户输入某些东西时,您需要将其存储在数组中,并在用户取消选择该单元格时将其删除。然后在用户选择下一个时添加。此数组将始终按用户选择的顺序排列。 – iDev

回答

1

声明一个字典作为,

@property (nonatomic, retain) NSMutableDictionary *selectedTextListDict; 

在viewDidLoad中,

NSMutableDictionary *selectedTextListDict = [[NSMutableDictionary alloc] init]; 

然后改变这些方法如,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

    if(selectedCell.accessoryType != UITableViewCellAccessoryCheckmark && count < 3) 
    { 
     NSString *rowKeyString = [NSString stringWithFormat:@"%d", indexPath.row]; 
     NSString *sectionKeyString = [NSString stringWithFormat:@"%d", indexPath.section]; 

     NSMutableDictionary *row = [self.selectedTextListDict valueForKey:sectionKeyString]; 
     if (!row) { 
      row = [[NSMutableDictionary alloc] init]; 
     } 
     [row setObject:selectedCell.textLabel.text forKey:rowKeyString]; 
     [self.selectedTextListDict setObject:row forKey:sectionKeyString]; 

     selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; 
     count ++; 
    } 

    else if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) 
    { 

     NSString *rowKeyString = [NSString stringWithFormat:@"%d", indexPath.row]; 
     NSString *sectionKeyString = [NSString stringWithFormat:@"%d", indexPath.section]; 

     NSMutableDictionary *row = [self.selectedTextListDict valueForKey:sectionKeyString]; 
     [row removeObjectForKey:rowKeyString]; 

     if ([[row allKeys] count] == 0) { 
      [self.selectedTextListDict removeObjectForKey:sectionKeyString]; 
     } else { 
      [self.selectedTextListDict setObject:row forKey:sectionKeyString]; 
     } 

     selectedCell.accessoryType = UITableViewCellAccessoryNone; 
     count--; 
    } 
} 


- (void)willMoveToParentViewController:(UIViewController *)parent 
{ 
    NSArray *selectedTextSectionKeysList = [self.selectedTextListDict allKeys]; 
    NSArray *sortedSelectedTextSectionKeysList = [selectedTextSectionKeysList sortedArrayUsingSelector:@selector(intValue)]; 
    int tempCount = 0; 

    for (NSString *sectionString in sortedSelectedTextSectionKeysList) { 

     NSMutableDictionary *rowDict = [self.selectedTextListDict valueForKey:sectionString]; 

     if (rowDict) { 

      NSArray *selectedTextRowKeysList = [rowDict allKeys]; 
      NSArray *sortedSelectedTextRowKeysList = [selectedTextRowKeysList sortedArrayUsingSelector:@selector(intValue)]; 

      for (NSString *rowString in sortedSelectedTextRowKeysList) { 

       tempCount++; 

       if (tempCount == 1) 
        chosenFieldsViewController.field0.placeholder = [rowDict valueForKey:rowString]; 

       else if(tempCount == 2) 
        chosenFieldsViewController.field1.placeholder = [rowDict valueForKey:rowString]; 

       else if(tempCount == 3) 
        chosenFieldsViewController.field2.placeholder = [rowDict valueForKey:rowString]; 
      } 

     } 
    } 
} 

编辑1:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"GoToModifyFieldsViewController"]) 
    { 
     ModifyFieldsViewController *modifyFieldsViewController = segue.destinationViewController; 
     modifyFieldsViewController.chosenFieldsViewController = self; 

     field0.placeholder = @""; 
     field1.placeholder = @""; 
     field2.placeholder = @""; 

     if(self.selectedTextListDict) 
      self.selectedTextListDict = [[NSMutableDictionary alloc] init]; 
    } 
} 

声明一个字典中ChosenFieldsViewController:如,

@property (nonatomic, retain) NSMutableDictionary *selectedTextListDict; 

在viewDidLoad中,

selectedTextListDict = [[NSMutableDictionary alloc] init]; 

因此,与其使用self.selectedTextListDict,使用:在ModifyFieldsViewControllerchosenFieldsViewController.selectedTextListDict

+0

我喜欢你的方法,但我们还没有到那里:(我将selectedTextList的声明转移到了'ChosenFieldsViewController'(parentVC)并将它们替换为'self。 '使用chosenFieldsViewController(类型的对象:'ModifyFieldsViewController'中声明的'ChosenFieldsViewController')。如果我一次选择三个字段,程序工作正常。如果我下次尝试修改字段,我不能这样做所以我修改了'prepareForSegue'中的代码,如果你不介意的话,我可以编辑你的答案来显示我改变了什么吗? –

+0

是的,你可以编辑它并作为更新添加它,我会接受它。它的工作,因为我打字整个代码没有使用Xcode,因此我期待一些明显的错误和逻辑错误谢谢接受。 – iDev

+0

谢谢!顺便说一句..我发现了一个错误。在编辑你的答案之前让我先回顾一下。字段出现的顺序对我至关重要。但是,如果用户随机选择单元格,则这些字段将按照它们的选择顺序显示。所以我试图解决这个问题。 –

0

- 如果是这样,为什么我看时,我向上滚动细胞似乎选择?

因为它们会在它们即将出现时自动创建。每次向上或向下滚动时,单元格都会被创建或重用。当它们消失时,它们会被破坏或标记为重用。

+0

感谢您的回答。那么,我们如何将这个概念扩展到识别单元格是否被选中,即使它们在tableView中不可见? –

+0

使用必要的元数据在视图控制器中保留数据结构。一个自定义对象的NSMutableArray应该做的伎俩。 – Minthos

+0

我也试过这个。这使得代码非常复杂。我必须编写一个代码,使用数组中的元素来编写每一行和每一部分的连接。例如,如果部分== 1,行== 4,objectAtIndex:7应该更新。因此,我不得不写一长串if-else阶梯。所以有一段时间我也调用了“plist”和“NSDictionary”的概念。 –