跟踪选定的单元格

时间:2012-11-22 23:38:56

标签: iphone ios xcode

在我的应用中,用户根据他/她选择的单元格更改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;
}

2 个答案:

答案 0 :(得分: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.selectedTextListDict,而不是ModifyFieldsViewController

答案 1 :(得分:0)

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

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