-3

I am trying to delete views that have a yellow shadow from my main viewcontroller.

It registers the number correctly but it doesn't delete. (It doesn't update the view I have tried to call setNeedsDisplay and all of those lines but the don't work. It only updates when you quit out of the app an reload it. It isn't in the managedobjectcontext but it stays in the view. Am I not releasing something?) If I had it so it only passed one item .. if you clicked on it to delete.. it would have worked but this isn't working with the shadows. Can you see why???

Update: I have views that are stored in core data (pages) and I want to delete the pages when they are selected and have a yellow shadow. If I need to how to I add the view to an array or something when it adds the shadow and then finds them when it needs to delete.

-(void)trashitems{
  for (NSString *itemKey in [itemViews allKeys]){
      UIView<CollectionViewItemView> *itemview = [itemViews objectForKey:itemKey];
      if ([itemview layer].shadowColor == [UIColor yellowColor].CGColor){
          NSLog(@"remove %i",[[NSDecimalNumber decimalNumberWithString:itemKey] unsignedIntegerValue]);

          if ([dataDelegate respondsToSelector:@selector(collectionView:canDeleteItemAtIndex:)]
              && [dataDelegate collectionView:self canDeleteItemAtIndex:[[NSDecimalNumber decimalNumberWithString:itemKey] unsignedIntegerValue]]
              && [dataDelegate respondsToSelector:@selector(collectionView:didDeleteItemAtIndex:)])
          {            
              [itemViews release];
              NSUInteger itemsCountBeforeDeletion = [dataDelegate countOfItemsInCollectionView:self];
              [dataDelegate collectionView:self didDeleteItemAtIndex:[[NSDecimalNumber decimalNumberWithString:itemKey] unsignedIntegerValue]];
              NSUInteger itemsCountAfterDeletion = [dataDelegate countOfItemsInCollectionView:self];
              if (itemsCountBeforeDeletion - 1 != itemsCountAfterDeletion){
                  [NSException raise:@"Collection View Deletion Exception" format:@"Count of items in collection view before deletion (%u) must equal one more than count of items in collection view after deletion (%u) but did not.", itemsCountBeforeDeletion, itemsCountAfterDeletion];
              }

          }
      }
  }
}
BDGapps
  • 3,318
  • 10
  • 56
  • 75
  • 4
    Your question is confusing, it's hard to tell what the connection is between removing a UIView and Core Data. Also your `trashitems` method doesn't call `removePageAtIndex` or vice-versa, so it's not apparent how or indeed if they are connected. Managed object contexts do not control UIViews, so what are you actually trying to do here? – Tom Harrington Jul 05 '11 at 16:16
  • I tried to update the code I forgot a part but I have views that are stored in core data (pages) and I want to delete the pages when they are selected and have a yellow shadow. – BDGapps Jul 05 '11 at 19:31
  • 1
    Comparing the layers' shadow colors is a poor way to determine what data you're going to delete. You should give the model objects some attribute that you can test, like `shouldDelete`, and let that drive the decision about what shadow color to use. Aside from your current scheme being a clear violation of MVC, there are too many ways that this could go wrong, e.g. are you sure that `[UIColor yellowColor].CGColor` always returns the same CGColorRef? Are you sure that nothing in the graphics system will change the color you assign? – Caleb Jul 05 '11 at 19:54
  • I log the index and its right, its just not letting go of the object – BDGapps Jul 05 '11 at 19:59
  • 3
    This is still extremely confusing. Storing views in Core Data is such a bizarrely bad design that I'm not sure I believe that's really what you're doing. If the immediate problem is that the view is still on the screen, it's probably because there's nothing in your code that ever removes it from the screen. Changes in Core Data have absolutely no effect on that. – Tom Harrington Jul 05 '11 at 22:46
  • how do i store it in an array or something – BDGapps Jul 05 '11 at 22:54
  • How do you store what in an array? NSArray works the same for any object derived from NSObject. I don't see what that has to do with anything else in this question though. – Tom Harrington Jul 05 '11 at 22:57
  • Instead of using the color when they tap it I can add it to an array or something and use that – BDGapps Jul 05 '11 at 22:59
  • I see. That would resolve the question of how to decide what to remove. You'd want to create an NSMutableArray somewhere, add items that you wish to remove, and then clear out the array when you no longer need to track those items for deletion. That doesn't get anywhere near what sounds like the real problem(s) though. – Tom Harrington Jul 05 '11 at 23:02
  • I think that is the problem so how do I add it to an NSMutableArray. And how do I get it out. I don't know how to do this could you please provide some code thanks – BDGapps Jul 05 '11 at 23:05
  • If you want to learn how to use NSMutableArray, please open a new question on that topic. – Tom Harrington Jul 05 '11 at 23:07
  • Why a new question I'll edit this one I just opened it. – BDGapps Jul 05 '11 at 23:09
  • Individual questions should be focused on one specific and well described issue even if several questions touch upon different issues in one small segment of code. Piling a lot of issues in one question just muddies the answers and makes it hard for others to use the questions for future reference. – TechZen Jul 06 '11 at 00:07

2 Answers2

1

Like Tom said, storing a view in Core Data is bizarre. To make a view disappear, it needs to be removed from the view hierarchy. The data should be separate from the view. I highly suggest reading up on the MVC (Model-View-Controller) design pattern.

Mike C.
  • 601
  • 3
  • 8
1

You've got a serious design problem here. This simply isn't going to work and you need to start over.

The Apple API uses the Model-View-Controller design pattern. It should have been called the Model-Controller-Interface design pattern because that better captures the true relationships. The model holds the data and data-behaviors, the controller connects the model to the interface and the interface provides the data to an external observer such as human looking at a command-line/GUI, another process or a remote server process.

You say that:

I am trying to delete views that have a yellow shadow from my main viewcontroller.

... but you are really not. The subviews themselves display some kind of data e.g. an image while the yellow shadow conveys to the user some kind of information about the state of that data e.g. a yellow shadow indicates that the image is older than some date. What you are really trying to do (in this example) is delete images that are older than a certain date and then you want the views of the user interface to reflect that change in the data.

Now the data of the image and its state of being older than a certain date belong in the Model. The controller reads the data from the model and configures the view and subviews according to the data provided. The controller doesn't know the logic of why the view should look like it does for any piece of represented data and the views don't know about the data at all, they just know what image they will display and what color their shadow is.

When you are using Core Data, you use it to create the model layer. You don't use it create the controllers, views or to store any state information directly related to the operation of the controllers or views. Ideally, a data model should be perfectly functional regardless of what kind of interface you eventually use i.e. it should work equally well with a command-line, a GUI, a webpage or an interprocess communication. It simply doesn't know or care about anything not directly related to the data and the associated logic (e.g. images older than a certain date need to be deleted) of how the data fits together.

So, you need to figure out what is data and data-logic and put that in Core Data while keeping the details of the UI in that displays that data in the controllers and views.

I can't really tell you exactly what you need to do because I don't know what data your app uses or what its data-logic is but I do know that you need to take all information concerning the actual views and their configuration out of Core Data.

TechZen
  • 64,370
  • 15
  • 118
  • 145