Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 13th, 2012  |  syntax: None  |  size: 3.64 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Array nil (even when addObject:) [closed]
  2. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4.     NSLog(@"TOUCHED CELL!");
  5.  
  6.     // Push the web view controller onto the navigation stack - this implicitly
  7.     // creates the web view controller's view the first time through
  8.     [[self navigationController] pushViewController:webViewController animated:YES];
  9.  
  10.     // Grab the selected item
  11.     entry = [[channel items] objectAtIndex:[indexPath row]];
  12.  
  13.     if (!entry) {
  14.         NSLog(@"!entry");
  15.     }
  16.  
  17.     // Construct a URL with the link string of the item
  18.     NSURL *url = [NSURL URLWithString:[entry link]];
  19.  
  20.     // Construct a request object with that URL
  21.     NSURLRequest *req = [NSURLRequest requestWithURL:url];
  22.  
  23.     // Load the request into the web view
  24.     [[webViewController webView] loadRequest:req];
  25.  
  26.     // Take the cell we pressed
  27.     // IMPORTANT PART
  28.     CELL = [tableView cellForRowAtIndexPath:indexPath];
  29.  
  30.     [webViewController setItem:entry];
  31.  
  32.     webViewController = nil;
  33.     webViewController = [[WebViewController alloc] init];
  34.     [entry release];
  35. }
  36.        
  37. -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
  38. {
  39.     cellToPassOn = nil;
  40.  
  41.     NSLog(@"Favouriting"); // YES I KNOW SPELLING
  42.  
  43.     // This is pretty simple, what we do is we take the cell we touched and take its title  and link
  44.     // then put it inside an array in the Favourites class
  45.  
  46.     Favourites *fav = [[Favourites alloc] init];
  47.     ListViewController *list = [[ListViewController alloc] init];
  48.     [self setCellToPassOn: [list CELL]];
  49.  
  50.     if (!item) {
  51.         NSLog(@"NILLED ITEM");
  52.     }
  53.  
  54.     [[fav arrayOfFavourites] addObject:[item autorelease]];
  55.     [fav setCell: cellToPassOn];
  56. }
  57.        
  58. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  59. {
  60.     NSLog(@"ROWS NO.");
  61.     NSLog(@"%i", [arrayOfFavourites count]);
  62.  
  63.     return [arrayOfFavourites count];
  64. }
  65.        
  66. if(arrayOfFavourites == NULL)
  67. {
  68.     arrayOfFavourites = [[NSMutableArray alloc] init];
  69. }
  70.        
  71. -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  72.  
  73. cellToPassOn = nil;
  74. NSLog(@"Favouriting"); // YES I KNOW SPELLING
  75.  
  76. // This is pretty simple, what we do is we take the cell we touched and take its title  and link
  77. // then put it inside an array in the Favourites class
  78.  
  79. // HERE you are creating a NEW fav
  80. Favourites *fav = [[Favourites alloc] init];
  81. // HERE you are creating a NEW list
  82. ListViewController *list = [[ListViewController alloc] init];
  83. //  SO HERE what is "CELL" doing, returning some constant or static object?
  84. [self setCellToPassOn: [list CELL]];
  85.  
  86. // HERE what is item and where does it come from
  87. if (!item) {
  88.     NSLog(@"NILLED ITEM");
  89. }
  90.  
  91. // Here you take an array of an object you just created and autoreleasing the item
  92. // this is not the regular way to handle memory management in Cocoa,
  93. // depending on what you are doing to item else where you could get item == deallocated pretty soon
  94. [[fav arrayOfFavourites] addObject:[item autorelease]];
  95. [fav setCell: cellToPassOn];
  96.  
  97. HERE you are releasing fav
  98. [fav release];
  99. HERE fav don't exist anymore as well as the array to which you've added something to it.
  100. [list release];
  101. item = nil;
  102. }
  103.        
  104. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  105. {
  106. // HERE everytime the tableview is asking for the numberOfRowsInSection, you create a new array
  107. // And that new empty array is replacing your old one.  
  108. // That is your "weird" thing, it's doing what you are asking it to do, set it back to a new empty array.
  109. arrayOfFavourites = [[NSMutableArray alloc] init];
  110.  
  111. NSLog(@"ROWS NO.");
  112. NSLog(@"%i", [arrayOfFavourites count]);
  113.  
  114. return [arrayOfFavourites count];
  115. }