我有一个tableview,其中包含用户选择的选项列表(这是一个编辑页面)。

tableview如下所示


  苹果UITableViewCellAccessoryCheckmark
  
  橙色UITableViewCellAccessoryNone
  
  菠萝UITableViewCellAccessoryCheckmark香蕉
  UITableViewCellAccessoryNone


- (void)viewDidLoad {

  self.mySavedFruitsArray = [myDBOperations getMyFruitsList:[appDelegate getDBPath]:self.myId];

}

/ Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------
{
    static NSString *CellIdentifier = @"PoemTypeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"]autorelease];

    }

   NSDictionary *aDict = [self.myFruitsArr objectAtIndex:indexPath.row];
   NSString *aValue = [aDict objectForKey:@"value"];
   NSString *aId = [aDict objectForKey:@"key"];
   cell.textLabel.text = aValue;
   cell.accessoryType = UITableViewCellAccessoryNone;

   NSDictionary *aSavedDict = [self.mySavedFruitsArray objectAtIndex:indexPath.row];
    NSString *Value = [aSavedDict objectForKey:@"value"];
    NSString *Id = [aSavedDict objectForKey:@"key"];
    if ( aId == Id ){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

  return cell;

}


mySavedFruitsArray-保存用户选择的水果。

myFruitsArr-这是常见的水果清单

现在我想知道如何显示与UITableViewCellAccessoryCheckmark匹配的单元格的mySavedFruitsArray

我的意思是,在此编辑视图中,我想显示带有用户选择选项的水果列表。

请让我知道该怎么做。

我这样尝试过,但没有用。

/ Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------
{
    static NSString *CellIdentifier = @"PoemTypeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"]autorelease];

    }

   NSDictionary *aDict = [self.myFruitsArr objectAtIndex:indexPath.row];
   NSString *aValue = [aDict objectForKey:@"value"];
   NSString *aId = [aDict objectForKey:@"key"];
   cell.textLabel.text = aValue;
   cell.accessoryType = UITableViewCellAccessoryNone;

   NSDictionary *aSavedDict = [self.mySavedFruitsArray objectAtIndex:indexPath.row];
    NSString *Value = [aSavedDict objectForKey:@"value"];
    NSString *Id = [aSavedDict objectForKey:@"key"];
    if ( aId == Id ){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

  return cell;

}


请注意self.mySavedFruitsArray可能并不总是等于myFruitsArr(因为用户只能选择一种水果)。

最佳答案

if ( aId == Id ){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


字符串比较是错误的。您应该这样比较字符串:

if([aId isEqualToString:Id]) {
....
}

10-08 11:28