将委托添加到自定义 UITableViewCell(访问错误) [英] Adding a delegate to a custom UITableViewCell (bad access error)

查看:21
本文介绍了将委托添加到自定义 UITableViewCell(访问错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

On this cell I have a button which need to fire a method in the ViewController where the UITableView is located on.

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

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.

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

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.

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

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.

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

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.

我的 customCell.h

My 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

.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

MyVC 在 .h 中正确实现委托

MyVC implements the delegate as proper in the .h

@interface MyVC : UIViewController <UITableViewDelegate, UITableViewDataSource, MyTableCellProtocoll>

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

and it also uses the one delegatemethod from MyTableCellProtocoll in the .m

- (void)didPressButton:(MyTableViewCell *)theCell
{
    NSLog(@"didPressButton");
}

我在 cellForRowAtIndexPath 中分配委托:

I assign the delegate in the 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;
}

这些都是我用于代理的所有位置.我想要它做的是更改按钮标题并在该单元格之后插入一些行(修改后的重复项)

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)

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

Hope this will clear things up a bit more.

正如评论部分中提到的那样,按钮方法在开始时没有被调用.使用 logcode 编辑代码.

The button method is not being called at start as was metioned in the comment section. Editted code with logcode.

按钮没有问题.是委托属性在某处搞砸了.通过断点、日志和评论,我确定了这一点.

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.

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

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.

最终好的,似乎所有的代表和按钮以及其他组件都不是问题.这是 heightForRowAtIndexPath: 把我搞砸了.总之感谢大家的支持!

FINAL 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!

推荐答案

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

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.

jrturton 好心地为我指明了正确的方向,对此他表示感谢.

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

这篇关于将委托添加到自定义 UITableViewCell(访问错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆