Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding a delegate to a custom UITableViewCell (bad access error)
    text
    copied!<p>I'm trying to add a custom delegate to a custom UITableViewCell of mine.</p> <p>On this cell I have a button which need to fire a method in the ViewController where the UITableView is located on.</p> <p>I'm doing all the usual steps to add a custom delegate but for some reason when I open the VC on runtime the app crashes and gives me a bad access error.</p> <p>When I comment all the delegate code it works properly so my guess is that something is wrong the way I add my delegate. When I leave my id property uncommented it seems to crash.</p> <p>I implement the protocol in the VC. I did assign the delegate to the cell in the cellForRowAtIndexPath:. All the delegate methods are in place. I've been using similar constructions in other classes but never in a subclass of UITableViewCells before.</p> <p>Before I post some code I would like to know if someone encountered this bug before. If and how he solved it or if it is even possible to make a custom delegate for an UITableViewCell subclass.</p> <hr> <p>EDIT:</p> <p>My customCell.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class MyTableViewCell; @protocol MyTableCellProtocoll &lt;NSObject&gt; -(void) didPressButton:(MyTableViewCell *)theCell; @end @interface MyTableViewCell : UITableViewCell { UIButton *myButton; id&lt;MyTableCellProtocoll&gt; delegationListener; } @property (nonatomic,retain) UIButton *myButton; @property (nonatomic,assign) id&lt;MyTableCellProtocoll&gt; delegationListener; - (void) buttonAction; @end </code></pre> <p>.m</p> <pre><code>#import "MyTableViewCell.h" @implementation MyTableViewCell @synthesize myButton; @synthesize delegationListener; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code self.myButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.myButton.backgroundColor = [UIColor clearColor]; self.myButton.frame = CGRectMake(5, 0, 10, 32); self.myButton.titleLabel.adjustsFontSizeToFitWidth = YES; [self.myButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:self.myButton]; } return self; } - (void) buttonAction { NSLog(@"buttonpressed"); [self.delegationListener didPressButton:self]; } @end </code></pre> <p>MyVC implements the delegate as proper in the .h</p> <pre><code>@interface MyVC : UIViewController &lt;UITableViewDelegate, UITableViewDataSource, MyTableCellProtocoll&gt; </code></pre> <p>and it also uses the one delegatemethod from MyTableCellProtocoll in the .m</p> <pre><code>- (void)didPressButton:(MyTableViewCell *)theCell { NSLog(@"didPressButton"); } </code></pre> <p>I assign the delegate in the cellForRowAtIndexPath:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.delegationListener = self; } return cell; } </code></pre> <p>These are all locations I use for the delegate. What I want it to do is change the buttontitle and insert some rows after that cell (modified duplicates)</p> <p>Hope this will clear things up a bit more. </p> <hr> <p>EDIT2: The button method is not being called at start as was metioned in the comment section. Editted code with logcode.</p> <hr> <p>EDIT3: There is nothing wrong with the button. It is the delegate property that is messed up somewhere. With breakpoints and logs and commenting i made sure of that.</p> <p>The code is being run up to the last element in my datasource list then it crashes. 1 step closer to the problem now i suppose.</p> <hr> <p>FINAL EDIT: Ok, it seems like all of the delegates and buttons and other components weren't the problem at all. It was the heightForRowAtIndexPath: screwing me over. Thanks for all of your support anyhow!</p>
 

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