单击CollectionViewCell将显示先前选择的单元格,而不是当前的单元格 [英] Click CollectionViewCell displays the previously selected cell instead of the current one

查看:75
本文介绍了单击CollectionViewCell将显示先前选择的单元格,而不是当前的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,当我单击一个单元格时,什么也没有发生,当我单击第二个单元格以显示上一个单元格(或我首先单击的那个单元格)的数据时,我认为它可以显示我错误的index.item(在详细信息视图中)

I have one problem, when I click on a cell, nothing happens, and when I click on a second cell that displays the data of the previous cell (or the one I clicked on first) in resumé I think it shows me the wrong index.item (in the detail view)

#define API_V3_CHANNEL_URL @"examples.json"

    @interface MSContestListViewController ()

    @end

    @implementation MSContestListViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.

        CGRect frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height);

        NSArray *colors = [[NSArray alloc] initWithObjects:
                           [UIColor colorWithRed:1 green:0.529 blue:0.357 alpha:1.0],
                           [UIColor colorWithRed:1 green:0.439 blue:0.357 alpha:1.0],
                           [UIColor colorWithRed:0.937 green:0.302 blue:0.357 alpha:1.0],
                           [UIColor colorWithRed:0.737 green:0.212 blue:0.357 alpha:1.0],
                           nil];

        CAGradientLayer *gradient = [DREasyGradient gradientWithFrame:frame
                                                          orientation:DRHorizontalGradient
                                                               colors:colors];


        [self.view.layer insertSublayer:gradient atIndex:0];


        _segmentedControl.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.2];

        [self fetchEntries];
        [self fetchEntriesWinner];
        [self fetchEntriesPhotos];

    }

    - (void)fetchEntries
    {
        NSString *searchURL = [API_V3_CHANNEL_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData *searchData = [NSData dataWithContentsOfURL:[NSURL URLWithString:searchURL]];
        NSDictionary *searchDict =[NSJSONSerialization JSONObjectWithData:searchData options:NSJSONReadingMutableContainers error:nil];
        //self.readArray = [searchDict objectForKey:@"CONTESTS"];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@", @"LIVE"];
        self.readArray = [[searchDict objectForKey:@"CONTESTS"] filteredArrayUsingPredicate:predicate];

        NSLog(@"%@", self.readArray);

    }

    - (void)fetchEntriesWinner
    {
        NSString *searchURL = [API_V3_CHANNEL_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData *searchData = [NSData dataWithContentsOfURL:[NSURL URLWithString:searchURL]];
        NSDictionary *searchDict =[NSJSONSerialization JSONObjectWithData:searchData options:NSJSONReadingMutableContainers error:nil];
        //self.readArray = [searchDict objectForKey:@"CONTESTS"];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@", @"ARCHIVED"];
        self.readArrayWinner = [[searchDict objectForKey:@"CONTESTS"] filteredArrayUsingPredicate:predicate];

        NSLog(@"winner array : %@", self.readArrayWinner);

    }

    - (void)fetchEntriesPhotos
    {
        NSString *searchURL = [API_V3_CHANNEL_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData *searchData = [NSData dataWithContentsOfURL:[NSURL URLWithString:searchURL]];
        NSDictionary *searchDict =[NSJSONSerialization JSONObjectWithData:searchData options:NSJSONReadingMutableContainers error:nil];
        //self.readArray = [searchDict objectForKey:@"CONTESTS"];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@", @"LIVE"];
        self.readArrayPhotos = [[searchDict objectForKey:@"CONTESTS"] filteredArrayUsingPredicate:predicate];

        NSLog(@"%@", self.readArray);

    }

    - (IBAction)segmentedControlAction:(id)sender
    {
        switch(_segmentedControl.selectedSegmentIndex)
        {
            case 0:

                if(self.readArray.count == 0)
                {
                    [self fetchEntries];
                } else {

                    [self.collectionView reloadData];
                }
                break;

            case 1:
                if(self.readArrayWinner.count == 0)
                {
                    [self fetchEntriesWinner];
                } else {
                    [self.collectionView reloadData];
                }
                break;

            case 2:
                if(self.readArrayPhotos.count == 0)
                {
                    [self fetchEntriesPhotos];
                } else {
                    [self.collectionView reloadData];
                }
                break;

            default:
                break;

        }

    }

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {

        switch (_segmentedControl.selectedSegmentIndex) {
            case 0:
                return [self.readArray count];
                break;

                case 1:
                return [self.readArrayWinner count];
                break;

                case 2:
                return [self.readArrayPhotos count];
                break;

            default:
                break;
        }

        return 0;
    }

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

        static NSString *CellIdentifier = @"pictureCell";

        MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

        cell.titleContest.adjustsFontSizeToFitWidth = YES;
        cell.titleContest.minimumScaleFactor = 0.5;

        cell.pictureImageView.layer.cornerRadius = 5;
        cell.pictureImageView.clipsToBounds = YES;

        cell.titleView.layer.cornerRadius = 5;
        cell.titleView.clipsToBounds = YES;

        switch (_segmentedControl.selectedSegmentIndex) {
            case 0: {
                NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
                NSString *stringImage = [searchResult objectForKey:@"featuredImage"];

                NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
                                         placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

                cell.titleContest.text = [searchResult objectForKey:@"description"];
                cell.statusContest.text = [searchResult objectForKey:@"status"];
                break;
            }
            case 1: {
                NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
                NSString *stringImage = [searchResult2 objectForKey:@"featuredImage"];

                NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
                                         placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

                cell.titleContest.text = [searchResult2 objectForKey:@"description"];
                cell.statusContest.text = [searchResult2 objectForKey:@"status"];
                break;
            }
            case 2: {
                NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
                NSString *stringImage = [searchResult3 objectForKey:@"featuredImage"];

                NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
                                         placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

                cell.titleContest.text = [searchResult3 objectForKey:@"description"];
                cell.statusContest.text = [searchResult3 objectForKey:@"status"];
                break;
            }

            default:
                break;
        }

        return cell;
    }

    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
    {

        NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
        NSString *videoID = [searchResult objectForKey:@"description"];
        NSString *stringImage = [searchResult objectForKey:@"featuredImage"];
        NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];

        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                                 bundle: nil];
        MSContestDetailViewController *yourController = (MSContestDetailViewController *)[mainStoryboard
                                                                                      instantiateViewControllerWithIdentifier:@"contestDetailViewController"];

        yourController.urlImage = image;
        yourController.contestName = videoID;
        yourController.contestTime = [searchResult objectForKey:@"drawDate"];

        [self.navigationController pushViewController:yourController animated:YES];

    }


    @end


推荐答案

您应该使用 didSelectItemAtIndexPath 函数而不是 didDeselectItemAtIndexPath

You should use the didSelectItemAtIndexPath function instead of the didDeselectItemAtIndexPath:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
    NSString *videoID = [searchResult objectForKey:@"description"];
    NSString *stringImage = [searchResult objectForKey:@"featuredImage"];
    NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                             bundle: nil];
    MSContestDetailViewController *yourController = (MSContestDetailViewController *)[mainStoryboard
                                                                                  instantiateViewControllerWithIdentifier:@"contestDetailViewController"];

    yourController.urlImage = image;
    yourController.contestName = videoID;
    yourController.contestTime = [searchResult objectForKey:@"drawDate"];

    [self.navigationController pushViewController:yourController animated:YES];

}

否则,当您第一次触摸某个项目时,什么也不会发生,因为什么都没有被取消选择。当您单击第二个项目时,选择第二个项目,但是选择第一个项目 de ,这将调用您当前的实现。但是实际上,您希望所有代码都在最初的 select 而不是 deselect 上进行。

Otherwise when you first touch an item, nothing happens because nothings gets deselected. When you then click a second item, the second one gets selected, but the first one gets deselected which invokes your current implementation. But you actually want all your code to happen on the initial select rather than the deselect.

这篇关于单击CollectionViewCell将显示先前选择的单元格,而不是当前的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆