【问题标题】:Change an UIImageView's alpha and show from UIActionSheet更改 UIImageView 的 alpha 并从 UIActionSheet 显示
【发布时间】:2014-02-05 16:43:01
【问题描述】:

在我的 UIActionSheet 中,我有一个按钮,将触摸的数组标记为收藏。当最喜欢一个数组时,我会在数组名称之前显示一个星号。收藏夹图标的 alpha 值为 0,通过触摸“添加为收藏夹”按钮,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;

        }

谢谢!

【问题讨论】:

  • 你在哪里更新收藏图标?可以发一下代码吗?
  • @67cherries 代码现已添加

标签: objective-c arrays uiimageview


【解决方案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,那么您可以调用reloadItemsAtIndexPaths:@[indexPath] 以提高处理效率。

【讨论】:

    【解决方案2】:

    您将希望在选择按钮时更新图像的 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多