سؤال

أحاول الإعداد UICollectionView برمجيا في وجهة نظري تحكم الذي يمتد UIViewController.لسبب ما، لا تظهر طريقة عرض مجموعتي على الإطلاق.وفيما يلي ما لدي.

لماذا لا يظهر؟أقوم بتوصيله إلى المفوض ومصدر البيانات وإضافته كعرض فرعي لـ self.view.ما هو المفقود في الكود الخاص بي؟

في .h ملف:

@interface MainViewController : UIViewController
{
    @private
    UICollectionView *_collectionView;
    NSMutableArray *_results; // data source array
}
@end

في .m ملف:

@interface MainViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *results;
@end

@implementation MainViewController

@synthesize collectionView = _collectionView;
@synthesize results = _results;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // some init stuff - nothing to do with collection view.
    }

    return self;
}

- (void)loadView
{
    self.results = [NSMutableArray array];
    UIImage *image1 = [UIImage imageNamed:@"img1.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"img2.jpg"];
    [self.results addObject:image1];
    [self.results addObject:image2];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView = collectionView;

    [self.view addSubview:self.collectionView];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [self.collectionView reloadData];

}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return [self.results count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *image = [self.results objectAtIndex:indexPath.row];    
    return CGSizeMake(image.size.width, image.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(50, 20, 50, 20);
}
هل كانت مفيدة؟

المحلول

لقد حصلت على أخطاء أثناء محاولة تشغيل التعليمات البرمجية الخاصة بك إلا إذا قمت بتغيير طريقةloadView إلى viewDidLoad - وفقًا للمستندات، ليس من المفترض أن تتصل بـloadView مباشرة.لتشغيل مصدر البيانات وأساليب التفويض، قمت بنقل الأسطر التي تحدد المفوض ومصدر البيانات إلى الذات أدناه حيث قمت بتعيين self.collectionView = CollectionView

    self.collectionView = collectionView;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

نصائح أخرى

لك numberOfSectionsInCollectionView: عائدات 0.لعرض المجموعة بقسم واحد، يجب عليك إما العودة 1 أو عدم تنفيذ هذه الطريقة.

كما أنني لا أستطيع رؤية المكان الذي تقوم فيه بتخصيص/init self.collectionView.

انتهى بي الأمر إلى التصنيف الفرعي UICollectionViewController بدلاً من UIViewController وتغيير طريقة init إلى:

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout

وقد نجحت.

ما عليك سوى ربط مندوب CollectionView ومصدر البيانات بـ ViewController في StoryBoard.أدخل وصف الصورة هنا

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top