gpt4 book ai didi

objective-c - NSInvalidArgumentException/EXC_BAD_ACCESS 与 UICollectionView

转载 作者:行者123 更新时间:2023-12-04 05:26:55 27 4
gpt4 key购买 nike

我有一个实现 UICollectionView 的 View Controller (它是委托(delegate)、数据源和流布局委托(delegate))。我在下一个 ScrollView 上显示它:

   UIViewController *playlistController = [[TNSPlaylistsController alloc] initWithNibName:@"TNSPlaylistsController" bundle:nil];
[self.scrollView addSubview:playlistController.view];

TNSPlaylistsController.h:
#import <UIKit/UIKit.h>

@interface TNSPlaylistsController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

TNSPlaylistsController.m:
#import "TNSPlaylistsController.h"

@interface TNSPlaylistsController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> // tried removing these on the m too

@end

@implementation TNSPlaylistsController
@synthesize musicStructure = _musicStructure, collectionView = _collectionView;

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

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


- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellData = [self.musicStructure objectAtIndex:indexPath.row];

static NSString *cellIdentifier = @"cvCell";

UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];

[titleLabel setText:cellData];

return cell;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.musicStructure = [[NSArray alloc] initWithObjects:@"aaa",@"aaa",@"aaa",@"aaa",@"aaa",@"aaa", nil];


UINib *cellNib = [UINib nibWithNibName:@"TNSPlaylistCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 100)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

[self.collectionView setCollectionViewLayout:flowLayout];

NSLog(@"%@\n%@\n%@\n%@\n%@", self, self.musicStructure, self.collectionView, flowLayout, cellNib);
}

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

@end

XIB 包含连接到实现文件的 UICollectionView。

问题是它在启动应用程序时独立地给出 EXC_BAD_ACCESS 或 NSInvalidArgumentException ( View Controller 加载到应用程序的第一个 View 上)。如果我将 View Controller 呈现为普通的模态视图,它可以正常工作

NSInvalidArgumentException 痕迹:
2012-10-27 15:32:47.144 Tunes[19255:c07] -[NSConcreteMapTable collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x76524c0
2012-10-27 15:32:47.146 Tunes[19255:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x76524c0'
*** First throw call stack:
(0x1598012 0x12a5e7e 0x16234bd 0x1587bbc 0x158794e 0x71b25f 0x71b2fa 0x71cbb4 0x6f4f14 0x70ab14 0x70b72b 0x707aea 0x71b38a 0x71b9cb 0x6f3273 0x23d92d 0x12b96b0 0x107fc0 0xfc33c 0x107eaf 0x2dc8cd 0x2251a6 0x223cbf 0x223bd9 0x222e34 0x222c6e 0x223a29 0x226922 0x2d0fec 0x21dbc4 0x21ddbf 0x21df55 0x226f67 0x2e75 0x1ea7b7 0x1eada7 0x1ebfab 0x1fd315 0x1fe24b 0x1efcf8 0x25fadf9 0x25faad0 0x150dbf5 0x150d962 0x153ebb6 0x153df44 0x153de1b 0x1eb7da 0x1ed65c 0x2bad 0x2ad5)
libc++abi.dylib: terminate called throwing an exception
(lldb)

EXC_BAD_ACCESS 痕迹:
asm code

我从来没有遇到过这种错误,所以我完全迷路了。我以几乎相同的方式在其他应用程序上实现了这个 UICollectionView(除了它不在 ScrollView 上,而是在主视图上)。有什么问题?谢谢!

最佳答案

我建议您在“诊断”选项卡的“编辑方案”中启用 NSZombie。您会发现所有 EXC_BAD_ACCESS 错误。

关于objective-c - NSInvalidArgumentException/EXC_BAD_ACCESS 与 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13100692/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com