情况是这样的,我自定义了一个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后,数据都正常,就是没法儿相应单击事件,求帮忙!
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后,数据都正常,就是没法儿相应单击事件,求帮忙!
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;@endcontroller的.h文件:#import <UIKit/UIKit.h>@interface LatestViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>@end
- (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;
}
你可以试试代码关联的方式。
例子:http://code4app.com/ios/%E7%AE%80%E5%8D%95UITableViewDemo/505548916803fa6b06000000
self.tableView.delegate=self;
self.tableView.datasource=self;如果都设置好,一般是能访问到didSelectedRowAtIndex: 协议方法的。还是检查一下,这种可能性要大一些。还有就是在UITableViewCell的- (void)setSelected:(BOOL)selected animated:(BOOL)animated 方法也可以响应点击事件。
谢谢各位啦,我找到根源了,- (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的原因,好坑爹啊,新手真的伤不起