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 上显示为 placeHolder。根据我的分析,我认为一旦向下滚动,单元格就会从内存中删除。因此,尽管选择了单元格,但它们无法显示在父 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;
}
4

2 回答 2

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在: as中声明一个字典,

@property (nonatomic, retain) NSMutableDictionary *selectedTextListDict;

在 viewDidLoad 中,

selectedTextListDict = [[NSMutableDictionary alloc] init];

因此,而不是使用self.selectedTextListDict,使用:chosenFieldsViewController.selectedTextListDictin ModifyFieldsViewController

于 2012-11-23T00:33:54.853 回答
0

- 如果是这样,为什么我在向上滚动时看到单元格被选中?

因为只要它们即将出现,它们就会自动创建。每次向上或向下滚动时,都会创建或重复使用单元格。当它们消失时,它们会被销毁或标记以供重复使用。

于 2012-11-22T23:52:44.407 回答