2014-02-05 78 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;