首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇怪的UICollectionView选择行为

奇怪的UICollectionView选择行为
EN

Stack Overflow用户
提问于 2013-02-14 09:40:48
回答 1查看 3.3K关注 0票数 10

我正在使用UICollectionView来显示一个菜单,并且以一种非常奇怪的方式选择了这些项目。

下面是我用来填充它们的静态数据:

代码语言:javascript
复制
self.menuItems = @[@{@"text" : @"First", @"image" : @"180-stickynote.png"},
                   @{@"text" : @"Second", @"image" : @"180-stickynote.png"},
                   @{@"text" : @"Third", @"image" : @"180-stickynote.png"},
                   @{@"text" : @"Fourth", @"image" : @"180-stickynote.png"},
                   @{@"text" : @"Fifth", @"image" : @"180-stickynote.png"},
                   @{@"text" : @"Sixth", @"image" : @"180-stickynote.png"}];

以及单元提供者,其中自定义子类仅附加到原型单元,并具有UILabelUIImageView

代码语言:javascript
复制
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CUMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCell" forIndexPath:indexPath];

    NSDictionary *cellInfo = [self.menuItems objectAtIndex:indexPath.row];

    cell.imageView.image = [UIImage imageNamed:[cellInfo valueForKey:@"image"]];

    cell.label.text = [cellInfo valueForKey:@"text"];

    return cell;
}

下面是did select row方法,记录标题和项目的行(它们都在第0节中):

代码语言:javascript
复制
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@: %d", [[self.menuItems objectAtIndex:indexPath.row] valueForKey:@"text"], indexPath.row);
}

最后,我的菜单的屏幕截图:

这是我从第一个到第六个,然后从第六个到第一个(第一,第二,第三,第四,第五,第六,第六,第五,第四,第三,第二,第一)(总共12次点击,注意第一次点击甚至没有注册,第二次第六点击也没有注册)的日志:

代码语言:javascript
复制
------------------------------------- FIRST TAP ON FIRST HERE
2013-02-13 19:38:37.343 App[1383:c07] First: 0  // second tap, on Second
2013-02-13 19:38:38.095 App[1383:c07] Second: 1 // third tap, on Third
2013-02-13 19:38:38.678 App[1383:c07] Third: 2  // fourth tap, on Fourth
2013-02-13 19:38:39.375 App[1383:c07] Fourth: 3 // fifth tap, on Fifth
2013-02-13 19:38:40.167 App[1383:c07] Fifth: 4  // so on
2013-02-13 19:38:41.751 App[1383:c07] Sixth: 5
------------------------------------- SECOND TAP ON SIXTH HERE
2013-02-13 19:38:42.654 App[1383:c07] Fifth: 4
2013-02-13 19:38:43.318 App[1383:c07] Fourth: 3
2013-02-13 19:38:44.495 App[1383:c07] Third: 2
2013-02-13 19:38:45.071 App[1383:c07] Second: 1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-14 09:52:11

这是因为您使用的是didDeselectItemAtIndexPath:方法而不是didSelectItemAtIndexPath:方法。这是一个容易犯的错误,特别是当你在输入代码时使用代码补全的时候。

票数 52
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14866461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档