Adding a delegate to a custom UITableViewCell (bad access error)

后端 未结 3 1316
离开以前
离开以前 2020-12-10 14:36

I\'m trying to add a custom delegate to a custom UITableViewCell of mine.

On this cell I have a button which need to fire a method in the ViewController where the UI

相关标签:
3条回答
  • 2020-12-10 14:56

    This question has been solved. The problem wasn't in the delegate and unrelated to the button issues in the other answers. The problem was lying in the access of a cell in the heightForRowAtIndexpath: and was easily solved once figured out.

    I was kindly pointed to the right direction by jrturton and for this he has my thanks.

    0 讨论(0)
  • 2020-12-10 15:02

    At first your interface called MyTableViewCell, implementation - iPadCheckTableViewCell. I think both should have the same name.

    And create button like this:

    self.myButton = [UIButton buttonWithType:UIButtonTypeCustom]; // instead of self.myButton = [[UIButton alloc] init]; - it's a memory leak
    
    0 讨论(0)
  • 2020-12-10 15:05

    I have heard that synthesized accessors should not be used during init. Try to do all the setup code using the ivars directly:

    myButton = [[UIButton alloc] init];         
    myButton.backgroundColor = [UIColor clearColor];         
    myButton.frame = CGRectMake(5, 0, 10, 32);         myButton.titleLabel.adjustsFontSizeToFitWidth = YES;         
    [myButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];         
    [contentView addSubview:myButton];
    

    If you change your button creation to [UIButton buttonWithType:UIButtonTypeCustom]; as beryllium rightly suggests, then you will also need to retain it.

    0 讨论(0)
提交回复
热议问题