Jazzer008
Jazzer008

Reputation: 39

Button sender.superview is showing as UIViewController instead of my CustomCell?

I have the button within the cell, it's got an outlet to the custom cell and it has an IBAction within my first view controller:

-(IBAction)AddButton:(UIButton *)sender{

ProductCell *cell = (ProductCell *)sender.superview;
UITableView *tableView - (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];

NSLog(@"Sender: %@, Super: %@", sender, sender.superview);

//[saveitemArray addObject: [itemArray objectAtIndex:(indexPath.row + 1)]];  
//[saveitemArray writeToFile:[self saveListPath] atomically:YES];
}

Stepping through the code reveals the sender as a UIButton but the variable 'cell' as the FirstViewController. Seems to be just skipping the cell and the table completely. What simple thing am I missing?

Update:

Ok so I've shown you the full IBAction, after commenting out the last two lines my cell variable is showing as CustomCell (The correct superview), but now my tableView variable is showing the FirstViewController superview instead of CustomTable? Your NSLog showed the same and so I changed it to show cell and cell.superview. Here is the result:

Sender: <UITableViewCellContentView: 0x716fb20; frame = (0 0; 320 43);     
gestureRecognizers = <NSArray: 0x71711d0>; layer = <CALayer: 0x716fbd0>>, Super:   
<ProductCell: 0x716f820; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize 
= W; layer = <CALayer: 0x716f990>>

Second Update:

It seems as thought the sender.superview is the CellContentView and not the Cell itself, and so when I try to get the indexPath it tries to use the cell as a table and the CellContentView as the cell. I didn't think it was necessary to go through the ContentView, I thought .superview from the button would go straight to the cell itself?

Answered:

Seems as though you have to go through the hierarchy in this order.

Button -> CellContentView -> Cell -> TableView

Upvotes: 3

Views: 2473

Answers (1)

Tieme
Tieme

Reputation: 65489

I Think something went wrong while hooking up the IBOutlets in interface-builder.

Set a breakpoint in the action and try logging your whole view recursively in the debugger to check it:

po [[self view] recursiveDescription];

Update:

It looks like you added the action to the cell instead of the button based on your update..

Update 2:

- (IBAction) AddButton:(UIButton *)sender{

    UIView *cellContentView = (UIView *)sender.superview;
    ProductCell *cell = (ProductCell *)cellContentView.superview;
    UITableView *tableView - (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];

}

or you could give the button a tag in this method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //your setup code here..

    cell.yourButton.tag = indexPath.row;

}

.. to get to the index path here if you have just 1 section:

- (IBAction) AddButton:(UIButton *)sender{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
}

Should do it!

Upvotes: 3

Related Questions