【问题标题】:How to Show UITableViewCellAccessoryCheckmark如何显示 UITableViewCell 附件复选标记
【发布时间】:2012-05-21 09:56:46
【问题描述】:

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

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 - 这是常见的水果列表

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

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

请告诉我该怎么做。

我试过这样,但没有用。

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

【问题讨论】:

    标签: iphone ios uitableview ios4 edit


    【解决方案1】:
    if ( aId == Id ){
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }    
    

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

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

    【讨论】:

    • 请注意 self.mySavedFruitsArray 可能不等于 myFruitsArr。
    • NSDictionary *aSavedDict = [self.mySavedFruitsArray objectAtIndex:indexPath.row];当 count 小于 myFruitsArr 或 indexpath.row 时,此行会崩溃
    【解决方案2】:

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

    【讨论】:

    • 我在滚动时崩溃
    • 您正在创建一个单元格标识符“PoemTypeCell”,如果单元格为 nil,则您正在使用另一个标识符“MyIdentifier”重新创建单元格。尝试使用相同的标识符
    【解决方案3】:

    在昨天(2014 年 6 月 8 日)下载的版本中,UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryNone 无效。它为我抛出了编译器错误。我认为您应该将其用作枚举,如下所示:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 2017-04-08
      相关资源
      最近更新 更多