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];
Related
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
I have the following code in my search method that updates the tableview.
for (int x = 0; x < [array count]; x++) {
//network stuff here
self.searchedReservations = [aSearchObjectType objectsFromServerDictionaries:aResultsArray];
[self.aTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}
Each object in the array represent a section in my table, for most cases the count will be 4, so 4 sections.
Right now, the code is loading the same data for all sections in the table's cells. Instead of the unique data for each section of the table.
Here is the code that loads the cells. I'm missing some logic that maps the data to the right section of the table.
MyClass *object = [self.searchedReservations objectAtIndex:iIndexPath.row];
cell.textLabel.text = object.customerFullName;
I assume you have the code below in your 'cellForRowAtIndexPath' method:
MyClass *object = [self.searchedReservations objectAtIndex:iIndexPath.row];
cell.textLabel.text = object.customerFullName;
Tables are organised into sections, then within each section there are rows. The rows start at zero again for each section.
If you are on section 0, loading cell 0 this code above is not taking the section number into account, only the row number - so it is loading:
[self.searchedReservations objectAtIndex:0]
to populate the cell.
When you are on section 1, loading cell 0 in section one, you are retrieving exactly the same value because you are just using the row number. ie. you're still loading
[self.searchedReservations objectAtIndex:0]
You may think that you are changing the value of the 'searchedReservations' object between the loading of each section (this is what it looks like you're trying to do in the loop above) but I'm not sure that this is working (as the code the populate searchedReservation doesn't seem to do anything different in each loop counter). Also the first time the table loads it will run through all of the rows in all of the sections anyway.
I think you need to either use the indexPath.section field as well as the indexPath.row field at the time you are populating the cell to ensure you are setting the appropriate row in the appropriate section.
Also use the debugger to see whats going on in your cellForRowAtIndexPath method and in your loop and make sure that you are getting/using a different 'searchedReservations' object for each incrementation of the loop and that this matches the 'searchedReservations' object in the cellForRowAtIndexPath method.
I have a UIScrollView and five UIViewControllers A,B,C,D,E.
The order of the UIViewController views to be shown in the scroll view depends on some pre-requisite (e.g. one day it might be B,A,E,D,C and the next E,A,D,B,C and so on).
How can I keep track of which UIViewController is to be displayed (as in something like tags for each UIViewController which can be stored in an array and updated)?
Is it possible to assign views to the scroll view in the dynamic fashion shown above?
you want to make out a way to permutate the 5 view controllers ?
maybe it helps to use the property of prime numbers.
it depends on the count of permutations you want.
but since there's only 5 view controllers, there can't be more than 5! = 120 permutations
what's good about 5 is that it's a prime number
so if you calculate (n*a) % 5,where a is a constant number from 0 to 4, and n is a variable from 1 to 5 you get a table like this :
a==0, 0,0,0,0,0
a==1, 1,2,3,4,0
a==2, 2,4,1,3,0
a==3, 3,1,4,2,0
a==4, 4,3,2,1,0
as you've seen, that's 5 completely different permutations, in which each number appears exactly once.
so, if you want to have 120 permutations
just apply a const number b (from 0 to 4), and calculate (n*a + b) % 5
then you'll get 5 times the previous result, which is 125 , but there are some repetitions, then the final result will be 120 permutations.
so, the only thing you have to do is to determine a and b with your prerequisite
and apply the following code:
int result = (n*a + b)%5;
static UIView *addedView = nil;
if(addedView != nil)
[addedView removeFromSuperview];
switch(result)
{
case 0:
[self.scrollView addSubview:VCA.view];
addedView = VCA.view;
break;
case 1:
[self.scrollView addSubview:VCB.view];
addedView = VCB.view;
break;
//and so on...
}
hope it helps
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 !
i need to hv an array of 20 objects.
initially array will hv 0 objects.
i hv add objbects 1 by 1,till it gets filled with 20 objects.
As soon as array gets 20 objects,and i try to insert a new object(for ex 21st object)
it should delete the 20th object and add itself to first position.
i hope i m giving u a clear picture,about wat i am looking for.
hope for a quick reply
regards
shishir
You could create your own class that uses an NSMutableArray for storage. When adding an item, first check whether there are already 20 objects in the array. If there are, remove the last object from the array. Then add the new object at the front.
- (void)addObject:(id)anObject
{
if ([dataArray count] == 20) {
[dataArray removeLastObject];
}
[dataArray insertObject:anObject atIndex:0];
}
The above will always add the newest object at the front of the array. I guess that is what you want.