Note that there are some explanatory texts on larger screens.

plurals
  1. POPassed UIButton parameter nil in target method
    primarykey
    data
    text
    <p>I'm trying to properly set the target of a UIButton touched event in a subclass of UITableCell (designed in IB) that will delete that cell. However, when ran in the simulator, I get the following error:</p> <blockquote> <p><strong>* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*</strong> - [__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'</p> </blockquote> <p>It gets to the method fine, but it always crashes right after the NSArray arrayWithObject line. It seems that the cause of this is because the button passed in to the target method is always nil.</p> <p>I'm assuming this is a memory issue, but I'm quite baffled as to how to fix it. Do I just need to create the cell entirely programmatically to get this to work, or is there an easy way to somehow specify the target of the buttons action as the main ViewController from Interface Builder?</p> <p>Here's where the cells are created in the view controller:</p> <pre><code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(tableView==songList){ static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; SongCell *cell = (SongCell *)[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; if(cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SongCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; [cell.addButton addTarget:self action:@selector(addToPlaylist:) forControlEvents:UIControlEventTouchUpInside]; } NSUInteger row = [indexPath row]; cell.songname.text = @"test";//[songListData objectAtIndex:row]; return cell; } return nil; } </code></pre> <p>And here's the target method:</p> <pre><code>-(void)addToPlaylist:(id)button{ SongCell *song = (SongCell *)[(UIButton *)button superview]; NSIndexPath *indexPath = [self.songList indexPathForCell:song]; NSArray *rowToMove = [NSArray arrayWithObject:indexPath]; [self.songList beginUpdates]; [self.songList deleteRowsAtIndexPaths:rowToMove withRowAnimation:UITableViewRowAnimationFade]; [self.songList endUpdates]; } </code></pre> <p>Any help you can offer would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload