开发者

Core Data: Deleting Views Stored in Core Data

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:)])
     开发者_JAVA技巧     {            
              [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];
              }

          }
      }
  }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜