Tôi đang cố gắng sử dụng UICollectionViewCell
, vì tất cả những gì tôi muốn hiển thị là hình ảnh. Tôi có thể thêm hình ảnh vào ô bằng cách sử dụng UIColor colorWithImage:
trên thuộc tính contentView
của UICollectionViewCell
.UICollectionView cellForItemAtIndexPath không đăng ký ô
Trong phương pháp loadView
của tôi, tôi đăng ký các tế bào như sau: [self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];
Dưới đây là phương pháp của tôi cellForItemAtIndexPath
:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// cell customization
return cell;
}
Khi tôi chạy nó, ngay khi nó hits dòng dequeue, nó đổ vỡ với lỗi sau:
*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MyCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Tôi mệt mỏi thiết lập ô tùy chỉnh và sử dụng i t như lớp học và tôi đã nhận được lỗi tương tự. UICollectionViewCell được phân lớp tùy chỉnh của tôi và không có gì được triển khai, ngoại trừ initWithFrame
mặc định. Đó là bởi vì tôi muốn thay đổi màu nền của khung nhìn. Tôi không chắc vấn đề là gì nhưng có thể ai đó vui lòng xem mã của tôi và giúp tôi không? Tôi đã cố gắng tìm ra điều này trong một thời gian và hoàn toàn không có may mắn chút nào.
Cảm ơn bạn vì điều này! Đã lái xe cho tôi hạt :) –
Nếu bạn muốn sử dụng Xib, bạn có thể tìm thấy câu trả lời trong tài liệu này [UICollectionView thêm UICollectionCell] (http://stackoverflow.com/questions/15184968/uicollectionview-adding-uicollectioncell). – Autobots