addObject: to array not working (array still nil)

北慕城南 提交于 2019-12-02 08:00:28

Why are you inializing the array in tableview:numberOfRowsInSection ? This will cause the array to be reset each time table view is reloaded. This could be your issue.

you are allocating your array in -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

try to allocate it somewhere else.

You could allocate the arrayOfFavorites in the tableView:numberOfRowsInSectionMethod, but then you first need to check if it is nil.

if( !arrayOfFavorites )
    arrayOfFavoriges = [[NSMutableArray alloc] init];

You should release it then in the dealloc method: [arrayOfFavorites release].

 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 cellToPassOn = nil;
 NSLog(@"Favouriting"); // YES I KNOW SPELLING
 // HERE creation of a Brand NEW empty Favourites instance
 Favourites *fav = [[Favourites alloc] init];
 // HERE creation of a Brand NEW empty ListViewController instance
 ListViewController *list = [[ListViewController alloc] init];
 // HERE we hope that the ListViewController as CELL other then nil when it is Brand NEW
 [self setCellToPassOn: [list CELL]];

 if (!item) {
     NSLog(@"NILLED ITEM");
 }

 [[fav arrayOfFavourites] addObject:[item autorelease]];
 [fav setCell: cellToPassOn];
 [fav release];
 // HERE the fav instance get deallocated and don't exist anymore
 [list release];
 // HERE the list instance get deallocated and don't exist anymore
 item = nil;
 }

In this code list and fav exist only in the body of this method, attempt to get to the value they have hold done to will failed, because list and fav doesn't exist outside that method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!