user198725878
user198725878

Reputation: 6386

How to Show UITableViewCellAccessoryCheckmark

I have a tableview which has a list of options the user has selcected( It is an edit page ).

the tableview looks as below

Apple UITableViewCellAccessoryCheckmark

Orange UITableViewCellAccessoryNone

Pineapple UITableViewCellAccessoryCheckmark Banana
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 - it holds the user selected fruits.

myFruitsArr - this has common list of fruits

now i would like to know how to display UITableViewCellAccessoryCheckmark for cell which matches with mySavedFruitsArray.

I mean , in this edit view i want to display the fruits list with user selected option.

Pls let me know how to do that.

I tried like this, but no use.

/ 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;

}

pls note self.mySavedFruitsArray may not be equal to myFruitsArr always ( because user may select only one fruit).

Upvotes: 0

Views: 836

Answers (3)

SimplGy
SimplGy

Reputation: 20437

In a version downloaded yesterday (6/8/14), UITableViewCellAccessoryCheckmark and UITableViewCellAccessoryNone is not valid. It was throwing compiler errors for me. I think you are supposed to use it as an enum, like so:

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

With this change, my code compiles and the behavior appears in the simulator. Sorry I don't know which version to look up (UIKit?), I'm new to iOS development.

Upvotes: 0

Ramy Kfoury
Ramy Kfoury

Reputation: 947

instead of checking if ( aId == Id ) which compares the strings as identical objects, use if ([aID isEqualToString:Id]) which compares strings

Upvotes: 1

graver
graver

Reputation: 15213

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

string comparison is wrong. You should compare strings this way:

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

Upvotes: 2

Related Questions