从UIActionSheet更改UIImageView的alpha和show

时间:2014-02-05 16:43:02

标签: objective-c arrays uiimageview

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

我遇到的问题是我必须重新打开ViewController才能显示明星。

当我按下“添加为收藏夹”按钮而不重新打开ViewController时,如何立即修复星标?

收藏夹图标alpha更新于:

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

这是更新alpha的代码:

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

        }

谢谢!

2 个答案:

答案 0 :(得分: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];
    }
}

如果您知道正在受欢迎的项目的indexPath,而不是调用reloadData,那么您可以调用reloadItemsAtIndexPaths:@[indexPath]以提高流程效率。

答案 1 :(得分:0)

选择按钮时,您需要更新图像的alpha值。您可以通过在UIViewController中实现UIActionSheetDelegate协议来实现此目的。您可以在View Controller的.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;