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

查看:111
本文介绍了将代理添加到自定义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>代理监听器

}

@property(nonatomic,retain)UIButton * myButton;

@property(nonatomic,assign)id< MyTableCellProtocoll>代理监听器

- (void)buttonAction;

@end

.m

  #importMyTableViewCell.h

@implementation MyTableViewCell

@synthesize myButton;
@synthesize delegationListener;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
//初始化代码

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中

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

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

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

我在cellForRowAtIndexPath中分配委托:


(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath :( NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @ 细胞;

MyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.delegationListener = self;
}

返回单元格;
}

这些都是我用于代表的所有位置。
我想要做的是更改buttontitle,并在该单元格之后插入一些行(修改的重复项)



希望这样可以更清楚一些。






EDIT2:
按钮方法在开始时不会在注释部分中被调用。编辑代码与日志代码。






EDIT3:
按钮没有任何错误。这是委托属性在某个地方搞砸了。使用断点和日志,并评论我确定的。



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






FINAL EDIT:
好​​吧,代表和按钮等组成部分根本不是问题。这是heightForRowAtIndexPath:拧紧我。
感谢所有的支持无论如何!

解决方案

这个问题已经解决了。
问题不在代理中,与其他答案中的按钮问题无关。问题在于在高度方面进入一个单元格,并且很容易解决。



我被jrturton指出正确的方向他非常感谢。


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 UITableView is located on.

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.

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.

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.

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.


EDIT:

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 implements the delegate as proper in the .h

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

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

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

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.


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


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.

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.


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!

解决方案

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.

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

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