NSMutableArray remove element at row and rearrange all the rows after that - iphone

As the question states, say I have an NSMutableArray containing 4 rows. I then delete row at position 2 by doing [array removeObjectAtIndex: 2] and I want all subsequent rows to be repositioned so that there is no empty slot in the NSMutableArray. (row 3 to become row 2, and row 4 to become row 3)
Is there a way to quickly do that?

This happens automatically when you call removeObjectAtIndex. From the class reference documentation:
To fill the gap, all elements beyond
index are moved by subtracting 1 from
their index.

It's the behavior of the NSMutableArray !

Related

TableView crash issue in swift3 while adding row

Here i am tryning to add a new row but it's crashing and giving the error "attempt to insert row 3 into section 2, but there are only 3 rows in section 2 after the update". Intially the emerCount is 2 and i am trying to add 3rd row.
var index = NSIndexPath()
if (emerCount<3){
emerCount += 1
index = NSIndexPath(row: emerCount, section: 2)
self.profileTableView?.insertRows(at: [index as IndexPath], with: .automatic)
self.profileTableView?.reloadData()
}
Two issues:
Before calling insertRows you have to insert the item in the data source array into the same location (index emerCount in section 2). This is mandatory.
Basically never call reloadData() right after insertRows. Inserting the row updates the table view with an animation. Remove reloadData().
Further be aware that indexes are zero-based and the third row is indeed inserted at index 2.
PS: Why is the table view optional? Is is created programmatically and displayed temporarily? And don't use NSIndexPath in Swift 3.
More context is needed. What is written in your data source (number​Of​Rows(in​Section:​)) methods? It seems that you number​Of​Rows(in​Section:​) returns 3 for the section but you want to add one more row.
Delete this line : self.profileTableView?.reloadData()

Add unknown number of elements to another cell array from a certain index on

I've data in the format of
a{1}(1,1)=1
a{1}(1,2)=3
a{1}(1,3)=0.5
a{1}(2,1)=1
a{1}(2,2)=5
a{1}(3,1)=2
a{1}(3,2)=7
...
Now I'd like to place item 1 & 2 of all rows in this cell into another cell array from an specific index on. How should I do this? for example to place column 1 & 2 of these unknown number of row in the cell array 'b' from index 5 on without repetition I used:
b{1}(1,(5:end + 1)) = unique(a{1}(:,(1:2)))
I'd like the output of cell array 'b{1}' from 5th elements becomes '1 2 3 5 7'.
However, it is false and gives the "Subscripted assignment dimension mismatch." error. I dont know where is the problem. Is my snippet right?
Any help is appreciated

iphone - Create NSMutableDictionary filled with NSMutableArrays dynamically

I have an array of objects. Each object has property "date" and "title".
I want to populate sectioned UITableView with those items like:
Section 1 - 2012.06.12 (taken from object.date)
Cell 1.1: Title 1 (taken from object.name)
Cell 1.2: Title 2
Cell 1.3: Title 3
...
Section 2 - 2012.06.13
Cell 2.1: Title 1
Cell 2.2: Title 2
..
Section 3 ..
I can do that by manually creating 1..n NSMutableArrays for all date combinations and filling them with object.name values. But the problem is I do not know how many date combinations there are, so it should be done dynamically. Also, the date property can repeat in different objects
My object structure is:
Object
-NSDate - date
-NSString - title
UPD:
I was thinking if it is possible to create NSDictionary, where the key would be my date and the object would be NSArray, which contains all my items for the key-date. But I do not know how to do that dynamically.
I hope I explained my question clearly enough.
Thank you in advance!
You can create arrays based on date.You have array of objects, so iterate through this array of objects to get distinct dates, as follows:
for(int i =0;i<[objectsArr count];i++)
{
if(![newDateArr containsObject:[objectsArr objectAtIndex:i].date])
{
[newDateArr addObject:[objectsArr objectAtIndex:i].date];
}
NSMutableArray *newTitleArray = [newTitleDictionary objectForKey:#"[objectsArr objectAtIndex:i].date"];
if(newTitleArray != nil)
{
[newTitleArray addObject:[objectsArr objectAtIndex:i].title];
}
else
{
newTitleArray = [[[NSMutableArray alloc] init] autorelease];
[newTitleArray addObject:[objectsArr objectAtIndex:i].title];
}
[newTitleDictionary setValue:newTitleArray forKey:#"[objectsArr objectAtIndex:i].date"];
}
where newTitleDictionary and newDateArr are declare outside this method.Now you can use both is newTitleDictionary and newDateArr to populate tableview.
If I understand you correctly, you want to put an object into an array and then use that array to populate a table view?
Just add the date object each time to the NSMutableArray.
[myArray addObject:dateObject];
Then when it comes to populating the table view..
DateObject *newDateObj = [myArray objectAtIndex:index];
I hope this helps and I understood your question
EDIT To answer now I understand a bit more.
Step 1
Check through the existing array of dates and see if there are any that match maybe by iterating through it using a for loop. Search online for how to compare NSDate.
Step 2 If it doesn't match any then insert it into the array as an array with just that date on it's own so the array count will be one. If it does match then insert it into the array along with that one making the array count 2 or more.
Step 3 When it comes to declaring the section amount for the table just return the dateHolderArray count.
Step 4 When declaring the amount of rows in each section, return the array count for the array thats inside the dateHolderArray.
Step 5 Display the content when it comes to populating the cells with information. It becomes just a task of getting the dates from the arrays using the section ids and row ids.
This is how I would do it, there are probably many other methods. Any questions just ask

Remove item at index

In my iCarousel, I use this to remove the items at index:
NSInteger index2 = carousel2.currentItemIndex;
[carousel2 removeItemAtIndex:index2 animated:YES];
[items2 removeObjectAtIndex:index2];
So the image in the view of the specific index is remove.
But I don't know how to reduce the count in my array to 1 or reduce the index count. How to do it?
sorry for the bad english.
When you remove any object from NSMutableArray the length of array automatically reduced by 1. You can check length of array by using
[carousel2 count];
and
[items2 count];
I did not get so much idea from your question. but I think you have to reload iCarousel after remove item.

Swapping nsmutabledata

I have one UIViewController containing a UITableView. Along with this I have 3 buttons and 4 Mutablable arrays.
Array "prime" is the primary datasource for the table view.
When I hit button a , b or c I write
prime= a
[table reloadData];
or
prime= b
[table reloadData];
The problem I have is that after say 2 button pushes prime is not equal to either a or b. It contents now contain the contents of a "and" b and it's contents will continue to accumulate after every assign.
How can I make 1 nsmutuablearray switch its contents to be that of another?
I know I can just remove all objects and then add more. I more wanted to know if its possible to make prime be a pointer to a b or c?
Try instead:
prime = [a copy];