我有一個包含自定義UICollectionViewCells(TestReceiptCell是類名稱)的UICollectionView。當我調用UICollectionViewCell dequeueReusableCellWithReuseIdentifier時,爲什麼會收到釋放的內存調用?
當自定義單元格只包含UILabel時,我沒有任何問題讓UICollectionView出現並加載自定義單元格。
然後我通過IB將UITableView添加到TestReceiptCell NIB文件中。我在TestReceiptCell.h中爲UITableView設置了一個引用出口,並在.m文件中合成。我將UITableView的委託和數據源設置爲包含UICollectionView的ViewController。
現在運行應用程序時,我就上三線在此塊EXC_ BAD_訪問異常:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"TestReceiptCell";
TestReceiptCell *cell = (TestReceiptCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; //exception thrown here
return cell;
}
我跑了殭屍儀器測試,發現瞭解除分配的內存調用源於此地。這是我第一次使用該儀器,因此我不確定如何從這裏進行調查。
僅供參考,這裏是代碼的一些相關部分:
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.myCollectionView registerNib:[UINib nibWithNibName:@"TestReceiptCell" bundle:nil] forCellWithReuseIdentifier:@"TestReceiptCell"];
// Setup flowlayout
myCollectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[myCollectionViewFlowLayout setItemSize:CGSizeMake(310, 410)];
[myCollectionViewFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.myCollectionView setCollectionViewLayout:myCollectionViewFlowLayout];
self.myCollectionView.pagingEnabled = YES;
}
我實現在ViewController.m的UITableView的數據源和委託方法文件以及但是我我不知道,如果問題就在這裏給出的EXC_BAD_ACCESS異常的起源:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"eventCell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"eventCell"];
}
return cell;
}
UPDATE:
我能夠得到這個,如果我改變cellForItemAtIndexPath來運行:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"TestReceiptCell";
//TestReceiptCell *cell = (TestReceiptCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
TestReceiptCell *cell = [NSBundle.mainBundle loadNibNamed:@"TestReceiptCell" owner:self options:nil][0];
return cell;
}
不過,我不離隊細胞,知道這是不是正確的方法。當dequeueReusableCellWithResueIdentifier創建一個新的單元格時,initWithFrame方法中的某處會出現問題。這是目前唯一的方法:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"TestReceiptCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
編輯:
如果我不選擇委託或爲實現代碼如下數據源,以tableviews的的CollectionView將加載。將委託/數據源附加到文件所有者的事情會導致錯誤。
您可能在'TestReceiptCell'和'FullReceiptCell'之間爲集合視圖單元混合了? – Yaman
啊 - 粗心的問題在我的最後問...忘了重新編碼正確的代碼。我修改了這個問題,謝謝。 – IkegawaTaro