views:

32

answers:

2

Hi

I have an array with 10 objects and I put this into a table. How can I edit one of the individual objects in the array or refresh it?

All the objects just hold text, so after I change the text how can I refresh the array? See where I am coming from?

Thanks.

A: 

You can, for example, show editing screen on table row selection, save editing and refresh table on hiding editing screen.

eviltrue
A: 

NSArray is an immutable class, which means that its contents can't be changed after it has been created. If you want to be able to change the contents of your array after it has been created, you should use an NSMutableArray instead. This subclass of NSArray provides additional methods such as:

  • insertObject:atIndex:
  • removeObjectAtIndex:
  • addObject:
  • removeLastObject
  • replaceObjectAtIndex:withObject:

Once you've updated your mutable array, you can reload your table by calling [self.tableView reloadData].

EDIT:

If you just want to change the value of one of the elements in your array without actually adding or removing any objects, then your array doesn't need to be mutable, but the object you're changing does. So if you have an array of NSString objects, you can change those to NSMutableString objects. Then you can use setString: on one of those objects to change its value.

cduhn
You're right, but I believe the OP was asking how to change the values of the objects contained in the array, which isn't affected by whether the array is mutable or not.
jlehr
You may be right. It's not clear what they're asking. I'll edit my reply to cover that possibility.
cduhn