Delete Sync between searchResultsTableView and mainTableView - uitableview

I have a simple question and I am looking for most efficient way to deal with this.
I have a main table (say mainTableView) and it has search bar controlled by searchResultsTableView.
My main table has a mutable array say mainItems of say 10 items.
When search is performed, the searchResultsTableView may contain say 3 items in different mutable array say searchedItems
now in that search controller I deleted 2 out of 3 items and I also delete from searchedItems and searchResultsTableView. These are delete 1 at a time.
So as I delete from searchedItems I also needs to delete from mainItems to keep in sync but the index would keep changing for every delete in mainItems so how do I know the original index to be deleted in mainItems?
Should I look for some dictionary based approach instead of array?

Cant you compare the objects inside array and remove using index.
Say like this, if the item deleted from searchedItems is deletedObj
int indexToDelete = -1;
for(i=0; i<[mainItems length]; i++){
Object * myMainObj = mainItems[i];
if (myMainObj isEqual:deletedObj) //this will work depends on the object inside your datasource
{
indexToDelete = i;
break;
}
}
if(indexToDelete != -1){
[mainItems removeObjectAtIndex:indexToDelete];
}

Related

How to append only few items into one array in Swift

Well, basically all im trying to do is I get a list of weather from the API which is an array of each node of the array represents a weather data every 3 hours, now all I want is to append only the first 6 items into my own array to load up the tableView with them.
so my question is : how can I append only six items into my own array? :)
since you have not posted any code, here is one example of how to do it.
var i = 0
for x in apiData{
if i >= 6 {
return
} else {
myArray.append(x)
i += 1
}
}
Note: this code will only append the first 6 items no matter what they are. If you want specific 6 items in the data the code will need to be modified, you will also need to provide more information in your question
Here is another way of doing so
if myArray.count == 6 {
return
}

cannot insert into NSMutableArray due to indexvalue being out of bounds

I have a MutableArray on this view called array and the object in question is detailItem, which has a property of rank (int). On this view, there's a text field displaying the rank and I want to be able to move the detailItem up and down the MutableArray by changing the rank.
So, for example let's say the detailItem has a rank of 3, which is index value of 2. If I change this in the text field to 3, I want the array to adjust and move it down one place. However, as I type in the value of rankField (the text field), it crashes the app since it automatically updates the value before I'm done editing. So, if I click on the text field and write 23 (planing on deleting the 2) or just press delete (now the value is nil) the app crashes with an uncaught exception.
Here's the code:
- (IBAction)rankFIeldTextChanged:(id)sender {
QueueMember *member = self.detailItem;
[self.array removeObjectAtIndex:self.detailItem.rank];
if (0<= [self.rankField.text intValue]<= self.array.count) {
[self.array insertObject:member atIndex:[self.rankField.text intValue]-1];
}
}
The if condition of making the text value in-between the array size and 0 seems to have no effect.
btw this is all in the detailsViewController which is connected to the main view controller via push segue. does it make more sense(or more better coding) to just set the new rank value in details and actually make the array changes in the mainviewcontroller.m?
The problem is that you are trying to do two boolean statements at once (which doesn't work). Change your if statement to something like:
if (0< [self.rankField.text intValue] && [self.rankField.text intValue] < self.array.count) {
//Insert your object here
}
else
{
//Add object here
}
Your current setup check to see if 0<= [self.rankField.text intValue], which will return true for all values greater than or equal to 0. Then it checks the result of that (YES:1, NO:0) if it's less than or equal to your array count. That will always return true if your array has anything in it. So basically your check will always return true.
Since it always returns true I could check for array object number 1000, your if statement says go for it, then I check and the array says "No way in heck!" and crashes your app.
EDIT: Updated my code snippet to take into account your array insertion line.
I'd just do this
- (IBAction)rankFIeldTextChanged:(id)sender {
QueueMember *member = self.detailItem;
[self.array removeObjectAtIndex:self.detailItem.rank];
if ((0<= [self.rankField.text intValue])&&([self.rankField.text intValue]<= self.array.count)) {
if (self.array.count >([self.rankField.text intValue]-1)){
[self.array insertObject:member atIndex:[self.rankField.text intValue]-1];
}
}
}
you are probably trying to insert at an index greater than the count of the array.

Objective-C how to pull several random items out of NSArray without getting duplicates? [duplicate]

This question already has answers here:
Getting a random object from NSArray without duplication
(3 answers)
Closed 7 years ago.
I have an array of random properties I would like to assign to equipment within the game I'm developing.
The code that I use below is returning an NSArray. I'm interested if there's way to get item indices from that array without getting duplicate values. The obvious solution is to create a mutable array with the returned array, do random, remove item that was returned and loop until the number of items is received.
But is there a different way of getting X random items from NSArray without getting duplicates?
//get possible enchantments
NSPredicate *p = [NSPredicate predicateWithFormat:#"type = %i AND grade >= %i", kEnchantmentArmor,armor.grade];
NSArray* possibleEnchantments = [[EquipmentGenerator allEnchantmentDictionary] objectForKey:#"enchantments"];
//get only applicable enchantments
NSArray *validEnchantments = [possibleEnchantments filteredArrayUsingPredicate:p];
NSMutableArray* mutableArray = [NSMutableArray arrayWithArray:validEnchantments];
NSDictionary* enchantment = nil;
if(mutableArray.count>0)
{
//got enchantments, assign number and intensity based on grade
for (int i = 0; i<3;i++)
{
enchantment = mutableArray[arc4random()%mutableArray.count];
[mutableArray removeObject:enchantment];
//create enchantment from dictionary and assign to item.
}
}
You can shuffle the array using one of the following techniques:
What's the Best Way to Shuffle an NSMutableArray?
Non repeating random numbers
Then, take the first X elements from the array.
Many years ago, I was working on card game and I realized that shuffling the deck was an inefficient way to get random cards. What I would do in your shoes is pick a random element, and then replace it with the element at the end of the array, like so:
#interface NSMutableArray (pickAndShrink)
- (id) pullElementFromIndex:(int) index // pass in your random value here
{
id pickedItem = [self elementAtIndex:index];
[self replaceObjectAtIndex:index withObject:[self lastObject]];
[self removeLastObject];
return pickedItem;
}
#end
The array will shrink by one every time you pull an element this way.
You could use a random number generator to pick a starting index, and then pick the subsequent indices based on some kind of math function. You would still need to loop depending on how many properties you want.
Eg:
-(NSMutableArray*)getRandomPropertiesFromArray:(NSArray*)myArray
{
int lengthOfMyArray = myArray.count;
int startingIndex = arc4random()%lengthOfMyArray;
NSMutableArray *finalArray = [[NSMutableArray alloc]init]autorelease];
for(int i=0; i<numberOfPropertiesRequired; i++)
{
int index = [self computeIndex:i usingStartingIndex:startingIndex origninalArray:myArray];
[finalArray addObject:[myArray objectAtIndex:index]];
}
return finalArray;
}
-(int)computeIndex:(int)index usingStartingIndex:(int)startingIndex
{
//You write your custom function here. This is just an example.
//You will have to write some code to make use you don't pick an Index greater than the length of your array.
int computedIndex = startingIndex + index*2;
return startingIndex;
}
EDIT: Even your computeIndex function could use randomness in picking the subsequent indices. Since you have a startingIndex, and another index, you could use that to offset your function so that you never pick a duplicate.
EDIT: If your array is very large, and the subset you need to pick is small, then rather than shuffle the entire array (maybe more expensive), you could use this method to pick the number of items you need. But if your array is small, or if the number of items you need to pick are almost the size of the array, then the godel9's solution is better.
You can use a mutable array and then remove them as the are selected, use something like random()%array.count to get a random index. If you don't want to modify the array then copy it with [array mutableCopy].

Edited UITableViewCell is being stored as a new cell

I have a table view that has a data source in sync with Core Data. However, I'm having a problem. Whenever I edit or delete a tableview cell, and I reload the view, I see a copy of the tableview cell that was there before it was edited. Here's some code to make it clearer.
When the view first loads, it tries to get all the "SOCommands" from "SOModule" which has a one-to-many relationship. Then, it converts it into "SOCommandTemp", so that I can work with them without altering the database.
_serverModuleCommands = [[NSMutableArray alloc]initWithArray:[self.serverModule.socommand allObjects]];
for(int i=0;i<[_serverModuleCommands count];i++)
{
SOCommandTemp* newTemp = [[SOCommandTemp alloc]init];
newTemp.commandName = ((SOCommand*)[_serverModuleCommands objectAtIndex:i]).commandName;
newTemp.sshCommand = ((SOCommand*)[_serverModuleCommands objectAtIndex:i]).sshCommand;
[_serverModuleCommands replaceObjectAtIndex:i withObject:newTemp];
}
Then, when I'm editing cells, I call methods such as these:
[_serverModuleCommands addObject:commandValues]; //commandValues is in the form of SOCommandTemp
[_serverModuleCommands replaceObjectAtIndex:_selectedCommandCell.row withObject:commandValues]; //_selectedCommandCell is an ivar that is cleared immediately after use
Then, when saving, I convert the array into SOCommand by doing this:
for(int j=0; j<[_serverModuleCommands count]; j++){
SOCommand* newCommand = [NSEntityDescription insertNewObjectForEntityForName:#"SOCommand" inManagedObjectContext:self.managedObjectContext];
newCommand.commandName = ((SOCommandTemp*)[_serverModuleCommands objectAtIndex:j]).commandName;
newCommand.sshCommand = ((SOCommandTemp*)[_serverModuleCommands objectAtIndex:j]).sshCommand;
newCommand.somodule = newModule;
}
However, before this is called, I want to make sure that I'm saving only one array item, since I added and editing one cell, so I do this:
NSLog(#"Going to Save: %#",[_serverModuleCommands description]);
And sure enough, I get only 1 array item. Then, I do save it, and exit the view controller. But when the first line:
_serverModuleCommands = [[NSMutableArray alloc]initWithArray:[self.serverModule.socommand allObjects]];
is called again, I'm getting two values in its description, one for the original and one for the edited.
Any help would be great!
~Carpetfizz
In your saving segment, you create a new SOCommand object no matter if it already exist.
Why not just use the actual objects (SOCommand) and edit them, this will not alter your DB information until you save the context.
It will save you some grieve swapping back and forth between your objects.
If you cannot edit in context, you should pass the existing item objectID to your "temp" objects and if it exist, fetch this object from DB and make the update to the existing item:
NSManagedObjectID* oID = ((SOCommandTemp*)[_serverModuleCommands objectAtIndex:j]).objectID;
if(oID) {
SOCommand* cmd = (SOCommand*)[context existingObjectWithID:oID error:nil];
if (cmd) { //no error fetching the object
//update `cmd` with your new values
}
}

Ordering objects under sections

Below is a screenshot of a UITableView that I want to order into by month. Currently their ordered by the first letter of the sub-heading in alphabetical order, what code should I use to make the events ordered into months? (By the way I have worked out how to order the sections)
Any help appreciated,
Seb
The best solution depends on what your data model looks like. Probably the simplest and most efficient, assuming your data model does not change very frequently, is just to have a routine that sorts each section's data in the order you want (based on each item's date). Create a list of pointers (or indices, again depending on your data structure's details), then all you have to do is look up the row index in the section's sorted index structure and display that element's data in your cellForRowAtIndexPath.
To optimize, you can keep a boolean "sorted" field in your data structure that is set to false whenever the data mutates in the table, then only sort on demand in the cellForRowAtIndexPath and set "sorted" to true.
EDIT: request for more step-by-step detailed description
OK, here's a bit more detail on how I'd go about it, assuming each of your sections is stored unsorted in a sortable container like NSMutableArray. Again the best solution depends on the details of your app like how frequently the section entries update and how you organize your data, and is there an upper bound on the number of entries in a section, etc. This is slightly different from my original suggestion in that the section's data container is directly sorted, and doesn't use an external ordering index.
Add a NSMutableSet sortedSections member somewhere convenient, close to your data model (inside its class would be best if you define it in a class)
// a section number is sorted if and only if its number is in the set
NSMutableSet *sortedSections;
When entries in sections are changed, added, or deleted, mark that section as unsorted by removing its number from the set
// just added, deleted, or changed a section entry entry
unsigned int sectionNum; // this section changed
...
NSNumber *nsNum = [NSNumber numberWithUnsignedInt:sectionNum];
[obj.sortedSections removeObject:nsNum];
In your cellForRowAtIndexPath (this may not be the most optimal place for this check, but it is a fast check and is the easiest location to get the sorting working), check if the section is sorted.
unsigned int sectionNum = [indexPath section];
NSNumber *nsNum = [NSNumber numberWithUnsignedInt:sectionNum];
if ( [obj.sortedSections containsObject:nsNum] )
// already sorted, nothing to do
else
{
// section needs to be resorted and reloaded
[mySectionData sortUsingFunction:compareSectionEntriesByDate context:nil];
// mark the section as sorted now
[obj.sortedSections addObject:nsNum];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
}
Here is an example sorting function, assuming your entry structures are of type NSDictionary and that you store the dates as NSDate objects with the key constant kEntryNSDate (which you would define yourself)
// sort compare function for two section entries based on date
//
static int compareSectionEntriesByDate( id e1, id e2, void *context)
{
NSDictionary *eDict1 = (NSDictionary *) e1;
NSDictionary *eDict2 = (NSDictionary *) e2;
NSDate *date1 = [eDict1 objectForKey:kEntryNSDate];
NSDate *date2 = [eDict2 objectForKey:kEntryNSDate];
int rv = [date1 compare:date2];
return rv;
}
That should be enough detail to get you going, good luck!

Resources