(菜鸟求助)自定义的UITableViewCell为什么不能响应事件

zhssuper 2012-11-12 03:17:32
情况是这样的,我自定义了一个UITableViewCell,显示数据什么的都一切正常,但是单击cell的时候没有事件响应,didSelectRowAtIndexPath这个方法进不去,UITableViewDelegate, UITableViewDataSource必须的方法也已经实现了,头痛啊,求大虾指点~急死~
UITableViewCell的xib:
图传不上来了,大致情况就是3个label和两个imageview
实现类(网上的一个例子):

#import "MessageTableCell.h"


@implementation MessageTableCell

@synthesize imageView;
@synthesize nameLabel;
@synthesize decLabel;
@synthesize locLabel;
@synthesize helpImgeView;

@synthesize image;
@synthesize helpImage;
@synthesize name;
@synthesize dec;
@synthesize loc;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code

}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (void)setImage:(UIImage *)img {
if (![img isEqual:image]) {
image = [img copy];
self.imageView.image = image;
}
}

-(void)setName:(NSString *)n {
if (![n isEqualToString:name]) {
name = [n copy];
self.nameLabel.text = name;
}
}

-(void)setDec:(NSString *)d {
if (![d isEqualToString:dec]) {
dec = [d copy];
self.decLabel.text = dec;
}
}

-(void)setLoc:(NSString *)l {
if (![l isEqualToString:loc]) {
loc = [l copy];
self.locLabel.text = loc;
}
}

- (void)setHelpImage:(UIImage *)img {
if (![img isEqual:helpImage]) {
helpImage = [img copy];
self.helpImgeView.image = helpImage;
}
}

@end

在表视图中用这个MessageTableCell后,数据都正常,就是没法儿相应单击事件,求帮忙!
...全文
3835 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenxufeiyue 2013-09-18
  • 打赏
  • 举报
回复
我看的自定义cell教程和你一样的- -
chenxufeiyue 2013-09-18
  • 打赏
  • 举报
回复
楼主 在tableview里面加了这个方法以后我还是不能通过点击cell 跳转,是为什么呢
a157147899 2012-11-13
  • 打赏
  • 举报
回复
应该是XIB文件设置的问题。 你可以试试代码关联的方式。 例子:http://code4app.com/ios/%E7%AE%80%E5%8D%95UITableViewDemo/505548916803fa6b06000000
zhssuper 2012-11-13
  • 打赏
  • 举报
回复
引用 8 楼 dabiaoyanjun 的回复:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 看看这个方法的实现。。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyCellIdentifier = @"MyCellIdentifier";
    
    static BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"MessageTableCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:MyCellIdentifier];
        nibsRegistered = YES;
    }
    
    MessageTableCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
    if (cell == nil) { 
        cell = [[MessageTableCell alloc] 
                initWithStyle:UITableViewCellStyleDefault 
                reuseIdentifier:MyCellIdentifier]; 
    }
    
    NSUInteger row = [indexPath row];
    
    cell.name = [dataList objectAtIndex:row];//datalist是一些假数据
    cell.dec = [dataList objectAtIndex:row];
    cell.loc = [dataList objectAtIndex:row];
    cell.image = [imageList objectAtIndex:row];
    cell.helpImage = [UIImage imageNamed:@"chat_message.png"];
    [cell setUserInteractionEnabled:YES];
    
    return cell;
}
冷卡卡西 2012-11-13
  • 打赏
  • 举报
回复
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 看看这个方法的实现。。
zhssuper 2012-11-13
  • 打赏
  • 举报
回复
或者哪位大虾有可以响应的自定义cell的简单例子,可以让小弟瞧瞧,我对比下自己找原因。
zhssuper 2012-11-13
  • 打赏
  • 举报
回复
引用 5 楼 leikaide 的回复:
把h头文件发来看看
cell的.h文件

#import <UIKit/UIKit.h>

@interface MessageTableCell : UITableViewCell{
    IBOutlet UIImageView * imageView;
    IBOutlet UILabel * nameLabel;
    IBOutlet UILabel * decLabel;
    IBOutlet UILabel * locLabel;
    IBOutlet UIImageView *helpImgeView;
}

@property (nonatomic, retain) IBOutlet UIImageView * imageView;
@property (nonatomic, retain) IBOutlet UILabel * nameLabel;
@property (nonatomic, retain) IBOutlet UILabel * decLabel;
@property (nonatomic, retain) IBOutlet UILabel * locLabel;
@property (nonatomic, retain) IBOutlet UIImageView *helpImgeView;

@property (copy, nonatomic) UIImage *image;
@property (copy, nonatomic) UIImage *helpImage;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *dec;
@property (copy, nonatomic) NSString *loc;

@end
controller的.h文件:

#import <UIKit/UIKit.h>

@interface LatestViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

@end
leikaide 2012-11-13
  • 打赏
  • 举报
回复
把h头文件发来看看
zhssuper 2012-11-13
  • 打赏
  • 举报
回复
引用 11 楼 zhanglei5415 的回复:
为了保险期间最好还是手动设置代理引用
self.tableView.delegate=self;
self.tableView.datasource=self;

如果都设置好,一般是能访问到didSelectedRowAtIndex: 协议方法的。还是检查一下,这种可能性要大一些。

还有就是在UITableViewCell的- (void)setSelec……



引用 10 楼 a157147899 的回复:
应该是XIB文件设置的问题。
你可以试试代码关联的方式。
例子:http://code4app.com/ios/%E7%AE%80%E5%8D%95UITableViewDemo/505548916803fa6b06000000


谢谢各位啦,我找到根源了,

- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}

因为这个方法,刚学这些方法都不熟悉,刚才删掉了这个方法就可以了,然后到开发手册查了下这个方法,有这么一句话:
Return Value
An index-path object that confirms or alters the selected row. Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don'€™t want the row selected.
是nil的原因,好坑爹啊,新手真的伤不起。。。
ReyZhang 2012-11-13
  • 打赏
  • 举报
回复
为了保险期间最好还是手动设置代理引用 self.tableView.delegate=self; self.tableView.datasource=self; 如果都设置好,一般是能访问到didSelectedRowAtIndex: 协议方法的。还是检查一下,这种可能性要大一些。 还有就是在UITableViewCell的- (void)setSelected:(BOOL)selected animated:(BOOL)animated 方法也可以响应点击事件。
zhssuper 2012-11-12
  • 打赏
  • 举报
回复
引用 3 楼 shine_code 的回复:
didSelectRowAtIndexPath这个方法 响应的是tableview对象的委托方法,确认tableview.delegate = self;这句已加
tableview.delegate和tableview.dataSource都在IB关联过了,要不我把代码发给大虾给小弟瞧瞧?代码不复杂,就一小段测试代码
shine_code 2012-11-12
  • 打赏
  • 举报
回复
didSelectRowAtIndexPath这个方法 响应的是tableview对象的委托方法,确认tableview.delegate = self;这句已加
zhssuper 2012-11-12
  • 打赏
  • 举报
回复
引用 1 楼 a157147899 的回复:
UserInterfaceEnabled=YES;?
加了,没用哟。。。
a157147899 2012-11-12
  • 打赏
  • 举报
回复
UserInterfaceEnabled=YES;?

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧