繁体   English   中英

来自笔尖的表格视图单元格中的集合视图

[英]Collection view in table view cell from nib

谁能告诉我,如何从笔尖的表格视图单元格内使用集合视图。 我有表格视图,自定义表格视图单元格,集合视图,自定义集合视图单元格。 在tableview的内部委托方法中,我添加了

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *cellId  = @"cell";

        CreateStoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            NSArray  *nib = [[NSBundle mainBundle] loadNibNamed:@"CreateStoryTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        [cell.collectionviwStory registerNib:[UINib nibWithNibName:@"StoryPicsCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"StoryPicsCollectionViewCell"];
        cell.collectionviwStory.dataSource=self;
        cell.collectionviwStory.delegate=self;

        // Configure layout
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setItemSize:CGSizeMake(100.0, 100.0)];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        [self setAutomaticallyAdjustsScrollViewInsets:NO];

        [cell.collectionviwStory setCollectionViewLayout:flowLayout];

        return cell;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier = @"StoryPicsCollectionViewCell";


    static BOOL nibMyCellloaded = NO;

    if(!nibMyCellloaded)
    {
        UINib *nib = [UINib nibWithNibName:@"StoryPicsCollectionViewCell" bundle: nil];
        [collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
        nibMyCellloaded = YES;
    }

    StoryPicsCollectionViewCell *cell = (StoryPicsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];

    return cell;
}

在我的自定义表格单元中

- (void)awakeFromNib {
    // Initialization code
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 9, 10);
    layout.itemSize = CGSizeMake(44, 44);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.collectionviwStory registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
    self.collectionviwStory.backgroundColor = [UIColor greenColor];
    self.collectionviwStory.showsHorizontalScrollIndicator = NO;
    [self.contentView addSubview:self.collectionviwStory];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



-(void)layoutSubviews
{
    [super layoutSubviews];

    self.collectionviwStory.frame = self.contentView.bounds;
}

- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
    self.collectionviwStory.dataSource = dataSourceDelegate;
    self.collectionviwStory.delegate = dataSourceDelegate;
    [self.collectionviwStory reloadData];
}

Nit我的代码在崩溃

 StoryPicsCollectionViewCell *cell = (StoryPicsCollectionViewCell *)
[
 collectionView dequeueReusableCellWithReuseIdentifier:identifier 
 forIndexPath:indexPath
];

崩溃日志

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier StoryPicsCollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
    0   CoreFoundation                      0x01ba6746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x0182fa97 objc_exception_throw + 44
    2   CoreFoundation                      0x01ba65da +[NSException raise:format:arguments:] + 138
    3   Foundation                          0x0149c720 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
    4   UIKit                               0x02a16f7b -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 1358
    5   UIKit                               0x02a172dc -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 160
    6   wedcraze                            0x00116f3f -[DashboardViewController collectionView:cellForItemAtIndexPath:] + 127
    7   UIKit                               0x02a07d5a -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 258
    8   UIKit                               0x02a09e96 -[UICollectionView _updateVisibleCellsNow:] + 4947
    9   UIKit                               0x02a0e6e1 -[UICollectionView layoutSubviews] + 281
    10  UIKit                               0x0238d57a -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 668
    11  libobjc.A.dylib                     0x01845771 -[NSObject performSelector:withObject:] + 70
    12  QuartzCore                          0x021b8e47 -[CALayer layoutSublayers] + 144
    13  QuartzCore                          0x021ac925 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 403
    14  QuartzCore                          0x021ac77a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    15  QuartzCore                          0x02108c52 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
    16  QuartzCore                          0x0210a0e5 _ZN2CA11Transaction6commitEv + 487
    17  QuartzCore                          0x0210a7fc _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    18  CoreFoundation                      0x01ac786e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    19  CoreFoundation                      0x01ac77b0 __CFRunLoopDoObservers + 400
    20  CoreFoundation                      0x01abd1ea __CFRunLoopRun + 1226
    21  CoreFoundation                      0x01abca5b CFRunLoopRunSpecific + 443
    22  CoreFoundation                      0x01abc88b CFRunLoopRunInMode + 123
    23  GraphicsServices                    0x046322c9 GSEventRunModal + 192
    24  GraphicsServices                    0x04632106 GSEventRun + 104
    25  UIKit                               0x022fa106 UIApplicationMain + 1526
    26  wedcraze                            0x000f348a main + 138
    27  libdyld.dylib                       0x07f60ac9 start + 1
    28  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

谁能帮我这个? 或为此建议一些教程

您需要将此代码放在自定义表格单元的awakeFromNib:中

    UINib *nib = [[UINib nibWithNibName:@"StoryPicsCollectionViewCell" bundle:[NSBundle mainBundle]];
    [self.collectionviwStory registerNib:nib forCellWithReuseIdentifier:@"StoryPicsCollectionViewCell"];

现在,在您有机会注册笔尖之前,便会进行双端电话通话。 希望尝试此操作会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM