【问题标题】:UICollectionView cell disappearing after seconds or when taping screenUICollectionView 单元格在几秒钟后消失或在点击屏幕时消失
【发布时间】:2014-04-02 10:22:32
【问题描述】:

我正在 iOS 7.0 上使用 Xcode 5.1 开发我的第一个 iPad 应用程序,我正在使用 UICollectionView。

Here is an sketch of my storyboard

当我单击导航栏中的第一个选项卡时,我的 CollectionViewController 包含在初始控制器的视图中。

几秒钟后或当我单击屏幕上的某个位置时,单元格会消失。

如果我把 CollectionViewController 作为初始视图控制器,单元格就可以了!

我不明白! 请帮忙。

#import "ActiviteViewController.h"
#import "AppDelegate.h"
#import "Embarcation.h"
#import "CollectionActiviteCellController.h"

@interface ActiviteViewController ()

@end

@implementation ActiviteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self; }

- (void) viewWillAppear: (BOOL) animated {
    [super viewWillAppear:animated];
    [[self view] setFrame: CGRectMake(0, 0, 1024, 618)]; }


-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES; }

- (void)viewDidLoad {
    [super viewDidLoad];
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;

    // Fetching Records and saving it in "fetchedRecordsArray" object
    self.embarcationsArray = [appDelegate getAllPayments];
    [self.collectionView reloadData]; }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. }

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    // Return the number of sections.
    return 1; }

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.embarcationsArray count]; }

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     CollectionActiviteCellController *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MonEmbarcation"
forIndexPath:indexPath];

    Embarcation * embarcation = [self.embarcationsArray objectAtIndex:indexPath.row];
    myCell.labelEmbarcation.text = [NSString stringWithFormat:@"%@, %@ ",embarcation.nom,embarcation.etat];

    NSLog([NSString stringWithFormat:@"%@, %@ ",embarcation.nom,embarcation.etat]);

    return myCell; }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a
little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller. }
*/

@end

【问题讨论】:

  • 您是否正确设置了数据源?当用于填充 collectionCells 的数组为空时,您可能正在某处执行 reloadData。您还注册了 CustomCell 吗?无论哪种方式。发布有关 CollectionView 的所有相关代码
  • 我发布了所有的revenant代码,我试图删除reloadData但没有任何改变
  • 无需删除 reloadData :O!您只需要检查“embarcationsArray”中的状态和数据。你能替换'[appDelegate getAllPayments];'用硬编码数组作为实验?此外,我没有看到您注册您的单元格(或提及任何类型的),所以您可以将此行添加到您的 viewDidLoad 方法中吗? [self.collectionView registerClass:[CollectionActiviteCellController 类] forCellWithReuseIdentifier:@"CollectionActiviteCellController"];。顺便说一句,我假设您正确设置了“CollectionActiviteCellController”以扩展“UICollectionViewCell”!

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

通过代码或 Storyboard 将 dataSource 出口连接到您的视图控制器。

代码:

self.collectionView.delegate = self;

或通过故事板 ctrl+从集合视图拖动到代表您的视图控制器对象的黄色矩形,然后选择“数据源”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多