我的应用程序基于ARC,我出于某种原因删除了ARC。现在UICollectionView中的应用程序崩溃我不明白如何发布img_array这里的对象
这是我的代码:
- (void)viewDidLoad
{
isShown = false;
[self.navigationController setNavigationBarHidden:YES];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
img_ary=[[NSMutableArray alloc]init];
img_ary= [NSMutableArray arrayWithObjects:@"car-image.png",@"image.png",@"car-image.png",@"image.png",@"car-image.png",@"image.png",@"car-image.png",@"image.png", nil];
[self collectioncreate];
img1_ary=[[NSArray alloc]init];
img1_ary=[NSArray arrayWithObjects:@"small-slider-images.png",@"small-slider-image-2.png",@"small-slider-image-2.png",@"small-slider-images.png",@"small-slider-image-2.png",@"small-slider-image-2.png",@"small-slider-images.png",@"small-slider-image-2.png",@"small-slider-image-2.png",@"small-slider-images.png",@"small-slider-image-2.png",@"small-slider-image-2.png",@"small-slider-images.png",@"small-slider-image-2.png",@"small-slider-image-2.png", nil];
nameArray = [[NSMutableArray alloc]init];
// nameArray = [NSMutableArray arrayWithObject:@"Top 5",@"Call",@"car",@"Buy",@"View Tutorial",@"MOI",@"Terms & Conditions",@"Private Policy",nil];
[nameArray addObject:@"Top 5"];
[nameArray addObject:@"Call"];
[nameArray addObject:@"Car"];
[nameArray addObject:@"Buy"];
[nameArray addObject:@"View Tutorial"];
[nameArray addObject:@"MOI"];
[nameArray addObject:@"Terms & Conditions"];
[nameArray addObject:@"Private Policy"];
[nameArray addObject:@"About Us"];
[nameArray addObject:@"Corporate Account"];
[nameArray addObject:@"4th Motor Envioroment"];
[nameArray addObject:@"Help & FAQ"];
[nameArray addObject:@"Contact Us"];
[nameArray addObject:@"Track Your Order"];
[nameArray addObject:@"Sell on $ Motor"];
[nameArray addObject:@"Adjust your Car"];
UIImage *searchFieldImage = [[UIImage imageNamed:@"search-1.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 17, 10)];
[search_bar setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];
//[self createScrollView1];
//[self createButton1];
}
-(void)collectioncreate1
{
cellNib = [UINib nibWithNibName:@"caracell" bundle:nil];
[collectionview registerNib:cellNib forCellWithReuseIdentifier:@"caracell"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(279, 244)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[collectionview setCollectionViewLayout:flowLayout];
collectionview.delegate=self;
collectionview.dataSource=self;
[collectionview reloadData];
}
-(void)collectioncreate
{
cellNib = [UINib nibWithNibName:@"carecell" bundle:nil];
[collectionview registerNib:cellNib forCellWithReuseIdentifier:@"carecell"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(279, 244)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[collectionview setCollectionViewLayout:flowLayout];
collectionview.delegate=self;
collectionview.dataSource=self;
[collectionview reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 10;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [img_ary count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// [self resetIdleTimer];
carecell * cell = (carecell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"carecell" forIndexPath:indexPath] ;
if(cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"carecell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.car_img.image = [UIImage imageNamed:[img_ary objectAtIndex:indexPath.row]];
**********************AT This point app Crash *********************
return cell;
}
请帮忙, 提前致谢
答案 0 :(得分:1)
此代码不正确:
img_ary=[[NSMutableArray alloc]init];
img_ary= [NSMutableArray arrayWithObjects:@"car-image.png",@"image.png",@"car-image.png",@"image.png",@"car-image.png",@"image.png",@"car-image.png",@"image.png", nil];
它目前创建两个 NSMutableArrays(两次调用alloc
),泄漏第一个,而不保留第二个。
您应该创建一个NSMutableArray,并通过保留它来保持对它的强引用(通过alloc
或retain
,但不能同时保留两者)
(编辑)
另外,你真的应该只使用ARC。这非常有帮助。
答案 1 :(得分:0)
如果你关闭ARC意味着,
就这样做。
@property (nonatomic,retain) NSMutableArray *img_ary;
并在实施中。
self.img_ary= [NSMutableArray arrayWithObjects:@"car-image.png",@"image.png",@"car-image.png",@"image.png",@"car-image.png",@"image.png",@"car-image.png",@"image.png", nil];
并删除此行img_ary=[[NSMutableArray alloc]init];
,在ARC和非ARC中都是浪费。