iphone - 跟踪选定的单元格

标签 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;
}

最佳答案

将字典声明为,

@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,不如使用: ModifyFieldsViewController 中的 chosenFieldsViewController.selectedTextListDict

关于iphone - 跟踪选定的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13521399/

相关文章:

iphone - iOS 版 ImageMagick 是否支持将 jpeg 图像转换为 GIF?

iphone - IOS中的巨大PNG图像

ios - IOS开发中的Segue是什么?

ios - 尝试获取视频文件的大小 (kb) 时出错

ios - 防止IOS通知菜单下滑

iphone - UIView只接受特定区域的触摸

iOS:即使路径正确,Twitter Stream API 仍为 Expanded_URL 返回 Null?

ios - UiTableView-将内容添加到相关部分

objective-c - 将 NSString 转换为十六进制

ios - 如何在 Swift 中发布请求