在我的应用程序中,用户根据他/她选择的单元格更改显示在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;
}
当用户输入文字时,您需要这样做'chosenFieldsViewController.field0.placeholder = selectedCell.textLabel.text;'。否则,您必须以某种形式将其存储在您的课堂中,以便在滚动时您不会丢失此信息。 – iDev
@ACB:我曾想过你提到的完全相同的解决方案。但它有点棘手。有三个部分,我希望这些字段的显示顺序与他们已选择的顺序相同。如果用户取消选择一个字段并从另一个部分选择一个字段,代码会变得稍微复杂。为了帮助你形象化,请参考:http://stackoverflow.com/questions/13460535/label-text-getting-mixed-up。事实上,我仍然感谢你帮助我解决了应用程序的一个主要问题。 :-) –
很高兴知道它有帮助。 :)你能分享一些更多的代码吗?实现上述目的的唯一方法是将其存储为当用户输入文本时。如果您可以分享更多的代码,那么我们可能会检查如何克服您面临的复杂情况。根据我的理解,当用户输入某些东西时,您需要将其存储在数组中,并在用户取消选择该单元格时将其删除。然后在用户选择下一个时添加。此数组将始终按用户选择的顺序排列。 – iDev