Jos*_*osh 10 xcode objective-c ios uistoryboard uicollectionview
我正在使用a UICollectionView
显示菜单,并且以非常奇怪的方式选择项目.
这是填充它们的静态数据:
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"}];
Run Code Online (Sandbox Code Playgroud)
和单元格提供程序,其中自定义子类只是附加到原型单元格并具有a UILabel
和a UIImageView
:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
这是do选择行方法,记录标题和项目的行(它们都在第0部分):
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@: %d", [[self.menuItems objectAtIndex:indexPath.row] valueForKey:@"text"], indexPath.row);
}
Run Code Online (Sandbox Code Playgroud)
最后,我的菜单截图:
这是我选择从第一到第六,然后从第六到第一(第一,第二,第三,第四,第五,第六,第六,第五,第四,第三,第二,第一)的项目的日志(总共12个,请注意,第一个水龙头甚至没有注册,第二个水龙头也没有注册):
------------------------------------- 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
Run Code Online (Sandbox Code Playgroud)
rde*_*mar 52
那是因为你使用的是didDeselectItemAtIndexPath:
方法而不是didSelectItemAtIndexPath:
.这是一个很容易犯的错误,尤其是当你输入代码时使用代码完成时.