Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Hi all,

is there a simple way to re-index your nsmutablearray's ?
I made a tableview and when I tap something it will go through a whole procedure and after, that object will be removed from the array, and the table needs to be reloaded IF there are any objects left.

But.. when reloading, the indexes of the objects has changed, so lets say I deleted objectAtIndex: 3 and my table will still have 4 objects left and I'm going to remove the 3rd object again, it will crash out.

Is there some simple command to re-index my array?
Instead of rebuilding the whole array again ?

Thanks!
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,764
8,465
A sea of green
Hi all,

is there a simple way to re-index your nsmutablearray's ?
I made a tableview and when I tap something it will go through a whole procedure and after, that object will be removed from the array, and the table needs to be reloaded IF there are any objects left.

Post your code that removes the object from the array.

If you use removeObjectAtIndex:, then the array's count will decrease by 1, and there will be no gap where the item was removed. Refer to the class reference doc.

If you're not using removeObjectAtIndex:, then post the code you're actually using.


Post your code that "reloads" the array after the object is removed.

In particular, show the code that returns the number of rows in a section. If there's one section and one array, the number of rows should be the array's count.

But.. when reloading, the indexes of the objects has changed, so lets say I deleted objectAtIndex: 3 and my table will still have 4 objects left and I'm going to remove the 3rd object again, it will crash out.
If there are 4 objects left, then their indexes will be 0, 1, 2, 3. So removing an object at index 3 will not crash out.

If you have observed an actual crash, then post the exact actual parameters. That is, the count of the array and the index where deletion was tried but failed.

In other words, post the actual problem, not a hypothetical. The hypothetical you posted doesn't have a problem.

Is there some simple command to re-index my array?
Instead of rebuilding the whole array again ?
If "re-index my array" means to close up the gap where the item was removed, see above. You may need to read the NSMutableArray reference documentation.

If you're getting an actual crash, post the actual code and the exception stack-trace.
 

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
In other words, post the actual problem, not a hypothetical. The hypothetical you posted doesn't have a problem.


If "re-index my array" means to close up the gap where the item was removed, see above. You may need to read the NSMutableArray reference documentation.

My appologies for this useless thread. I find your answer really awesome.
My working time was ending and was strugling into this 'hypothetical' issue.
As I just implementated an function in my terminal, I did not had an actual test with multiple objects for the function, only 1.

I just found some 'gap' in my spare time and done some testing with theoretical options and you are 100% right.

Again my appologies for this thread, I really appriciate that you are taking this much time for such an formal and honest answer on my thread.

I'm being pushed a little and have too less time for my apps and am actually rushing into it. Actually its all getting a bit complicated and was really expecting an issue, but could not find an good testing environment for my app with multiple issue's which needs the function I'm working on.

Thanks again, and again my appologies.:rolleyes:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.