2014-02-05 77 views
0

在我的UIActionSheet中,我有一个按钮将触摸的数组标记为收藏夹。当数组最喜欢的时候,我有一个星号显示在数组的名字之前。最喜欢的图标有一个alpha 0,通过触摸“Add as Favorite”按钮,alpha变为1.数组位于一个UIViewController中,每个单元显示1个数组。更改UIImageView的alpha并从UIActionSheet中显示

我遇到的问题是我不得不重新打开ViewController以显示星星。

我该如何修复,以便在按下“添加为收藏夹”按钮时立即显示星号,而无需重新打开ViewController?

的收藏图标阿尔法是根据更新:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

这是更新的阿尔法代码:

if ([FavoriteArray containsObject: [mainArray objectAtIndex:indexPath.row]]) 
     { 
      favoriteImage.alpha = 1; 

     } 

谢谢!

+1

你在哪里更新最喜欢的图标?你能发布代码吗? – 68cherries

+0

@现在添加67个代码 – ThomasGulli

回答

1

您需要监视何时已执行操作工作表,并相应地更新收集视图。示例实现如下:

UIActionSheet* actionSheet = ...; 

// Set yourself as the action sheet delegate, so you can monitor when events occur 
actionSheet.delegate = self; 

... 

// This is called when a user selects an item within the action sheet 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    // kFavouriteButtonIndex is the index of the action sheet item to favourite 
    if (buttonIndex == kFavouriteButtonIndex) { 
     // Reload your collection view to update the cells 
     [self.collectionView reloadData]; 
    } 
} 

与其说reloadData的,如果你知道这是被收藏最多的项目的indexPath,那么你可以调用reloadItemsAtIndexPaths:@[indexPath],使过程更加高效。

0

当您选择一个按钮时,您将需要更新图像的阿尔法。您可以通过在您的UIViewController中实现UIActionSheetDelegate协议来完成此操作。你可以在你做这个视图控制器的.m文件:

@interface MyViewController : UIViewController <UIActionSheetDelegate> 

然后覆盖在.m文件的actionSheet:clickedButtonAtIndex:方法:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    //make sure that the button pressed is the one which should change the image alpha: 
    if(buttonIndex == 2){ //put the button index in the place of "3"  

    //your code to update the alpha here 
    } 
} 

确保您的UIActionSheet的委托设置为self:

myActionSheet.delegate = self;