将委托添加到自定义UITableViewCell(错误访问错误)

Tot*_*mus 10 delegates exc-bad-access objective-c uitableview

我正在尝试将自定义委托添加到我的自定义UITableViewCell中.

在这个单元格中,我有一个按钮,需要在ViewController中触发UITableView所在的方法.

我正在做所有常规步骤来添加自定义委托但由于某种原因,当我在运行时打开VC时,应用程序崩溃并给我一个错误的访问错误.

当我评论所有委托代码时,它正常工作,所以我的猜测是我添加委托的方式有问题.当我将我的id属性取消注释时,它似乎崩溃了.

我在VC中实现协议.我确实将委托分配给cellForRowAtIndexPath:中的单元格.所有委托方法都已到位.我一直在其他类中使用类似的构造,但从未在UITableViewCells的子类中使用过.

在我发布一些代码之前,我想知道以前有人遇到过这个bug.是否以及如何解决它或者甚至可以为UITableViewCell子类创建自定义委托.


编辑:

我的customCell.h

#import <UIKit/UIKit.h>

@class MyTableViewCell;

@protocol MyTableCellProtocoll <NSObject>

-(void) didPressButton:(MyTableViewCell *)theCell;

@end

@interface MyTableViewCell : UITableViewCell
{

    UIButton *myButton;

    id<MyTableCellProtocoll> delegationListener;

}

@property (nonatomic,retain) UIButton *myButton;

@property (nonatomic,assign) id<MyTableCellProtocoll> delegationListener;

- (void) buttonAction;

@end
Run Code Online (Sandbox Code Playgroud)

.M

#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
Run Code Online (Sandbox Code Playgroud)

MyVC在.h中实现了委托

@interface MyVC : UIViewController <UITableViewDelegate, UITableViewDataSource, MyTableCellProtocoll>
Run Code Online (Sandbox Code Playgroud)

它还使用.m中来自MyTableCellProtocoll的一个委托方法

- (void)didPressButton:(MyTableViewCell *)theCell
{
    NSLog(@"didPressButton");
}
Run Code Online (Sandbox Code Playgroud)

我在cellForRowAtIndexPath中分配委托:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

这些是我用于代表的所有位置.我想要它做的是更改buttontitle并在该单元格之后插入一些行(修改后的副本)

希望这会让事情变得更加清晰.


EDIT2:按钮方法在开始时没有被调用,如评论部分所述.带有日志代码的编辑代码.


EDIT3:按钮没有问题.委托属性在某处搞砸了.通过断点和日志以及评论,我确保了这一点.

代码正在运行到我的数据源列表中的最后一个元素然后崩溃.我猜想现在距离问题更近了一步.


最终编辑:好的,似乎所有的代表和按钮以及其他组件都不是问题所在.这是heightForRowAtIndexPath:搞砸了我.无论如何,感谢您的支持!

Tot*_*mus 4

这个问题已经解决了。问题不在代表中,与其他答案中的按钮问题无关。问题出在 heightForRowAtIndexpath: 中的单元格的访问上,一旦弄清楚就很容易解决。

jrturton 善意地为我指出了正确的方向,对此我向他表示感谢。

  • 您能分享一下解决方案吗? (3认同)