开发者

Transferring objects from one array to another

开发者 https://www.devze.com 2023-03-30 04:14 出处:网络
I\'m faced with a small problem. I have a UITableView with 2 sections: folders and individual posts (inks). Posts are saved into a NSMutableArray and folders are an array of arrays that contain posts.

I'm faced with a small problem. I have a UITableView with 2 sections: folders and individual posts (inks). Posts are saved into a NSMutableArray and folders are an array of arrays that contain posts. When you hit the Edit button, check mark boxes appear on each cell that allow each post to be checked. Once an item is checked, it is removed from the inks array and moved into a temporary array. After you select which folder you'd like to move the posts to, the items from the temporary array are copied into one of the folder arrays. The reason I'm using a temporary array to transfer the items is incase the user unchecks an item so that this way it's not removed from the actual array.

-(IBAction)checkMarkSelected:(id)sender
{
    if(numberTimesChecked%2==0) { //even times, check
        [appDelegate.inksToFolder addObject:[appDelegate.inks objectAtIndex:[sender tag]]];
        [tempArray removeObjectAtIndex:[sender tag]]; //tempArray is initialized with the contents of appDelegate.inks in the viewDidAppear method, so its just a duplicate
    }
    else //odd times, no check
    {
        [appDelegate.inksToFolder removeObjectAtIndex:[sender tag]];
        [tempArray addObject:[appDelegate.inks objectAtIndex:[sender tag]]];
    }
    numberTimesChecked++;  }

-(IBAction)moveToFolderPressed:(id)sender
{
    NSMutableArray *arrayOfInks=[appDelegate.folders objectAtIndex:[sender tag]];
    [arrayOfInks addObjectsFromArray:appDelegate.inksToFolder];
   开发者_运维百科 [appDelegate.folders replaceObjectAtIndex:[sender tag] withObject:arrayOfInks];
}

So the problem I am facing is what to do if the user hits the check box more than once. If the user presses it an even amount of times, this means the checkmark is present, and if an odd amount of times, then there is no checkmark, and these items should not be touched. However, when an item is checked and say this item is at index 4 in the inks array, this item is moved to the tempArray at index 0. If it is then unchecked, how will it know where to remove the item from in the tempArray? So if I check index 4 then index 2 then index 7 then uncheck index 4, it will attempt to remove the object from index 4 from the inksToFolder array, but that index doesn't exist. Does anyone know a solution, or even a better way to implement this whole thing?


There is probably a better way to do this whole thing. I'd keep track of which rows are checked and then only perform the array movements once the user commits the action.

But if you'd just like a workaround to finish your current technique, then maybe you could use NSMutableDictionary as your temporary store.

Write the object to the dictionary:

[myDictionary setObject:myObject forKey:someKey];

an example for the key:

[myDictionary setObject:myObject forKey:[NSNumber numberWithInt:[sender tag]];

You can use the [sender tag] as the key in the dictionary if you wrap it in an NSNumber first (keys of NSMutableDictionary must conform to the NSCopying protocol, and NSNumber does just that).

Find an object based on it's key in the dictionary:

[myDictionary objectForKey:someKey];

Remove an object based on it's key in the dictionary:

[myDictionary removeObjectForKey:someKey];

After the user has done all their checking and unchecking, and they commit the move, then to get the objects out of your temporary dictionary and back into the array you can:

Retrieve all keys from the dictionary:

NSArray *keysArray = [myDictionary allKeys];

Loop through this whole array and insert the dictionary's objects back into your original array:

[myDictionary objectForKey:someKey];

Use NSNumber -intValue to convert the key back into an integer to perform the insert back into your original array:

[myOriginalArray insertObject:myObject atIndex:[someKey intValue]];
0

精彩评论

暂无评论...
验证码 换一张
取 消