如何显示UITableViewCellAccessoryCheckmark

时间:2012-05-21 09:56:47

标签: iphone ios uitableview ios4 edit

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

tableview如下所示

  

Apple 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(因为用户可能只选择一个水果)。

3 个答案:

答案 0 :(得分:2)

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

字符串比较错误。你应该这样比较字符串:

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

答案 1 :(得分:1)

而不是检查(aId == Id)是否将字符串作为相同的对象进行比较, 使用if([aID isEqualToString:Id])来比较字符串

答案 2 :(得分:0)

在昨天(2014年6月8日)下载的版本中,UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryNone无效。这给我带来了编译器错误。我认为你应该把它用作枚举,就像这样:

    if item.completed {
        cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
    } else {
        cell!.accessoryType = UITableViewCellAccessoryType.None
    }

通过此更改,我的代码将编译并且行为将显示在模拟器中。 抱歉,我不知道要查找哪个版本(UIKit?),我是iOS开发的新手。