Ios 从核心数据在表中设置默认UITableViewCellAccessoryCheckmark,然后允许捕获新选择

Ios 从核心数据在表中设置默认UITableViewCellAccessoryCheckmark,然后允许捕获新选择,ios,core-data,uitableview,Ios,Core Data,Uitableview,我有一个SoundTableViewController,其中有3个静态行指示声音选择选项。我想在行上加载一个选中的/默认的UITableViewCellAccessoryCheckmark,其中包含核心数据中对象的相应名称/字符串值。不太难。但是,一旦成功添加了复选标记,我想让用户能够选择和取消选择任何其他声音,并添加/删除复选标记。一旦用户做出最终选择,我想将所选声音添加/更新到Core Data中的相应对象并保存。此外,如果用户离开SoundTableViewController并返回,我

我有一个
SoundTableViewController
,其中有3个静态行指示声音选择选项。我想在行上加载一个选中的/默认的
UITableViewCellAccessoryCheckmark
,其中包含核心数据中对象的相应名称/字符串值。不太难。但是,一旦成功添加了复选标记,我想让用户能够选择和取消选择任何其他声音,并添加/删除复选标记。一旦用户做出最终选择,我想将所选声音添加/更新到Core Data中的相应对象并保存。此外,如果用户离开
SoundTableViewController
并返回,我希望保留此选择

我能够使用
didSelectRowAtIndexPath
中的indexath标记来实现行的“检查和取消检查”。在核心数据中加载属性时,我能够获得要“检查”的默认选择。然而,我不知道如何让这两件事同时发生

这是我的
cellForRowAtIndexPath
didSelectRowAtIndexPath
方法

cellforrowatinexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"sound";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

if(cell == nil )
{
    cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Getting file name from path for title of cell
NSString *fileName = [soundsArray objectAtIndex:indexPath.row];
fileName = [[fileName lastPathComponent] stringByDeletingPathExtension];
cell.textLabel.text = fileName;
NSLog(@"FILENAME HAS A VALUE OF:   %@", fileName);

if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
    {

        //If sound has been selected previously, set sound selection
        if ([cell.textLabel.text isEqualToString:self.selectedSound])
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }

        //if no sound has been selected previously, set 'Bells' as default sound
        if ([cell.textLabel.text isEqualToString:@"Bells"])
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
    }
else
    {
    cell.accessoryType = UITableViewCellAccessoryNone;
    }    

//The following is the original code to 'check and uncheck' rows
/*
if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
*/

return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

self.lastIndexPath = indexPath;
[self.tableView reloadData];

}
didSelectRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"sound";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

if(cell == nil )
{
    cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Getting file name from path for title of cell
NSString *fileName = [soundsArray objectAtIndex:indexPath.row];
fileName = [[fileName lastPathComponent] stringByDeletingPathExtension];
cell.textLabel.text = fileName;
NSLog(@"FILENAME HAS A VALUE OF:   %@", fileName);

if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
    {

        //If sound has been selected previously, set sound selection
        if ([cell.textLabel.text isEqualToString:self.selectedSound])
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }

        //if no sound has been selected previously, set 'Bells' as default sound
        if ([cell.textLabel.text isEqualToString:@"Bells"])
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
    }
else
    {
    cell.accessoryType = UITableViewCellAccessoryNone;
    }    

//The following is the original code to 'check and uncheck' rows
/*
if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
*/

return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

self.lastIndexPath = indexPath;
[self.tableView reloadData];

}
我假设我需要的逻辑应该全部进入
cellforrowatinexpath
。然而,也许最好的方法是让
didSelectRowAtIndexPath
进行新的选择并保存到核心数据

再一次,我想:

  • 开始时在选定/默认行上加载复选标记

  • 允许用户从表中选择任何声音,并对当前声音进行复选标记 仅选定行

  • 最终选择后,将选定行从核心数据保存到当前对象

  • 任何见解或指针都将不胜感激。

    首先,您甚至在创建单元格之前就已经设置了单元格文本

    // Getting file name from path for title of cell
    NSString *fileName = [soundsArray objectAtIndex:indexPath.row];
    fileName = [[fileName lastPathComponent] stringByDeletingPathExtension];
    cell.textLabel.text = fileName;
    NSLog(@"FILENAME HAS A VALUE OF:   %@", fileName);
    
    
    if(cell == nil )
    {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    

    我想你的第一个手机里可能没有文字?是吗?另外,当视图第一次出现时,您是如何初始化self.lastIndexPath和self.selectedSound的?

    单元格是先前创建的,但我移动了代码以消除混淆。创建的所有单元格都没有问题。第一个单元很好<代码>@property(非原子,保留)nsindepath*lastIndexPath被合成并
    @属性(非原子,强)NSString*selectedSound也是<代码>self.selectedSound=self.soundTransferString从核心数据中的我的对象设置我选择的声音字符串。