title: How to add null or nil value in "dynamic NSArray" in Objective-C
body: <p>I am facing a problem regarding to add <code>Null or nil</code> value in <code>NSArray</code>. Actually I am adding <code>Null</code> value because my array count is not same. I am adding three array in <code>Custom TableViewCell</code> two array are from webservice and one array is from database. I am saving <code>IndexPath</code> in <code>core data</code> and then retrieving it.</p>
  
  <p><a href="https://i.stack.imgur.com/ofL61.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ofL61.png" alt="enter image description here"></a> </p>
  
  <p>As shown in Image I am saving <code>indexPath</code> in String and convert it in <code>NSInteger</code> from <code>DidSelectAtIndexPath</code> and show it in <code>cellForRowAtIndexPath</code>. My problem is, it's getting override because it is stored in string. So that I save it in <code>coredata</code>a and retrieve it but I get problem for mismatch count of array in <code>cellforrowatindexpath</code>. My code is like this</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
           static NSString *STI=@"STI";
          AuditTableViewCell *cell = (AuditTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
          if (cell == nil)
          {
              NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AuditTableViewCell" owner:self options:nil];
              cell = [nib objectAtIndex:0];
              cell.accessoryType=UITableViewCellAccessoryNone;
          }
          cell.audittitlelbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
          cell.auditdesclbl.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
  
          NSManagedObject *device2 = [devices objectAtIndex:indexPath.row];
          NSMutableArray *Checkarray=[devices valueForKey:@"Key"]; // Hear in this array I am getting Index count problem
          NSLog(@"Device =%@",device2);
          NSLog(@"Check Array =%@",Checkarray);
          if(indexPath.row == CurrentIndexPath)
          {
              cell.listlbl.text=GlobalString;
              [cell setBackgroundColor:[UIColor greenColor]];
          }
  
          return cell;
  }
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
             CurrentIndexPath=indexPath.row;
              NSLog(@"Current Index Path =%ld",(long)CurrentIndexPath);
              GlobalIndexPath = [[NSString stringWithFormat: @"%ld", (long)CurrentIndexPath] mutableCopy];
  
              NSManagedObjectContext *context = [self managedObjectContext];
  
              if (self.device) {
                  // Update existing device
                  [device setValue:GlobalStringChk forKey:@"Key1"];
                  [device setValue:GlobalIndexPath forKey:@"Key"];
  
  
              } else {
                  // Create a new device
                  NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
                  [newDevice setValue:GlobalStringChk forKey:@"Key1"];
                  [newDevice setValue:GlobalIndexPath forKey:@"Key"];
              }
  
              NSError *error = nil;
              // Save the object to persistent store
              if (![context save:&amp;error]) {
                  NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
              }
  
  
  
              [Audittable reloadData];
  
  
  
              NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
              NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
              self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
  }
  </code></pre>
  
  <p>This is my code hear I am saving <code>indexpath</code> in <code>coredata</code> and retrieving it in array. I have a problem for count of array in <code>cellforrowatindexpath</code>. How can I add <code>Null or nil</code> value in array so that it count get same. My Array is a dynamic array.The main problem is that I need to change the colour of cell if user click on it.when I store the value in NSInteger and convert it into indexpath I can change the colour But only for one cell at a time.When I click on other cell integer value get override. So for that I save it to core data and retrive but when I fetch core data array in cellforrowatindexpath it crash because of different count .Thanks in Advance!</p>