1

I've got one NSMutableArray with 100+ characters in which I've populated a table with:

int rowCount = indexPath.row;
Character *character = [self.characterArray objectAtIndex:rowCount];

cell.textLabel.text = character.name;
cell.detailTextLabel.text = character._id;

I've then got a separate NSMutableArray with an _id and neighbour values in, however this array only has around 8 values with random id's in.

Basically I want to check if the character._id exists in the second array, It'll display a checkmark on the table.

I've tried doing:

Neighbour *neighbour = [self.neighbourArray objectAtIndex:rowCount];

if (character._id == neighbour._id && neighbour.neighbour == 1){
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
         cell.accessoryType = None;
}

But it crashes the App (because it only has 8 values of 100, I'm guessing)

Is there an easy way around this or a better way to check altogether?

Thanks, any constructive advise would be greatly appreciated, I'm brand new to Xcode.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235

2 Answers2

4

You can use KVC to avoid the enumeration:

BOOL result = [[neighbours valueForKey:@"_id"] containsObject: character._id];
serrrgi
  • 644
  • 3
  • 7
0

Currently you are checking Character object at same index of Neighbour, you should check it in whole array,

you can try like this

-(BOOL)check :(Character *) character
{

for(Neighbour *neighbour in  self.neighbourArray )
{
  if(character._id == neighbour._id && neighbour.neighbour == 1)
   return TRUE;
}

  return FALSE;

}

now just call this method in tableviewMethod

int rowCount = indexPath.row;
Character *character = [self.characterArray objectAtIndex:rowCount];

if ([self check:character]){
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
         cell.accessoryType = None;
}
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121