0

在我发布问题本身之前,我需要说明这是一个越狱应用程序。这就是为什么我在文件系统中的“奇异”文件夹中写入内容。

让我们继续。

这是我的 cellForRowAtIndexPath 方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"pluginCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
    }
    if(indexPath.row == 0)
    {
        cell.textLabel.text = @"default";
    }else
    {
        //Get the plugin's display name.
        NSBundle *currentPlugin = [[NSBundle alloc] initWithPath:[NSString stringWithFormat:@"/Library/Cydeswitch/plugins/%@", [plugins objectAtIndex:indexPath.row - 1], nil]];
        cell.textLabel.text = [[currentPlugin localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"];
        if(cell.textLabel.text == nil)
        {
            //No localized bundle, so let's get the global display name...
            cell.textLabel.text = [[currentPlugin infoDictionary] objectForKey:@"CFBundleDisplayName"];
        } 
        [currentPlugin release];
    }

    if([[[cell textLabel] text] isEqualToString:[settings objectForKey:@"pluginToExecute"]])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        currentCell = [cell retain];
    }
    return cell;
}

如您所见,此方法使用一个名为 currentCell 的成员来指向当前“选定”的单元格。这是一个选项表,用户在任何时候都应该只能有一个带有复选标记附件图标的单元格。

当用户选择另一个单元格时,他正在更改一个选项,并且复选标记应该从当前单元格中消失并出现在新出现的单元格中。我这样做:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    currentCell.accessoryType = UITableViewCellAccessoryNone;
    [currentCell release];
    currentCell = [[self tableView:tableView cellForRowAtIndexPath:indexPath] retain];
    NSLog(@"CURRENT CELL %@", currentCell.textLabel.text);
    currentCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

但它不起作用。当我点击另一个单元格时,复选标记正确地从旧单元格中消失,但它从未出现在新单元格中。

我知道选择工作正常,因为那里的 NSLog 可以很好地打印新单元格的文本。

我之前尝试过跟踪 indexPath,但它根本不起作用。当我尝试使用 indexPaths 而不是指向单元格的指针时,当用户点击单元格时,什么都没有发生(至少在我目前的方法中,复选标记从旧单元格中消失了)。

我认为这与 cellForRowAtIndexPath 有关,因为如果我一直指向单元格,复选标记就会消失,但是由于某种原因,当尝试从使用 cellForRowAtIndexPath 获取的单元格中更改附件类型时,它似乎根本不起作用。

任何帮助将不胜感激。

4

2 回答 2

2

错字?试试这个:

currentCell = [[self.tableView cellForRowAtIndexPath:indexPath] retain];
于 2013-05-08T15:00:17.767 回答
1

您不能按照自己的方式跟踪最后选择的单元格。细胞被重复使用。使用 ivar 来跟踪indexPath适用于您的数据的密钥或其他密钥。

然后在该didSelect...方法中,您使用保存的 indexPath 或键获得对旧单元格的引用。在该cellForRow...方法中,您需要根据当前 indexPath 是否与您保存的 indexPath 匹配来设置正确的附件类型。

最后,不要调用您自己的委托/数据源方法。当获取一个单元格的引用时,直接向表格视图询问它。

currentCell顺便说一句 - 你在你的cellForRow...方法中过度保留。除非这是您第一次进行分配,否则无需在该方法中保留所有内容。

于 2013-05-08T15:01:36.553 回答