在 tableView 上使用 CustomCell,如何调用 didSelectRowAtIndexPath?
我正在使用 CustomCells 填充 UITableView,并尝试调用 didSelectRowAtIndexPath 。这是自定义单元格的标题。
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
IBOutlet UILabel *bizNameLabel;
IBOutlet UILabel *addressLabel;
IBOutlet UILabel *mileageLabel;
IBOutlet UIImageView *bizImage;
}
@property (nonatomic, retain) UILabel *bizNameLabel;
@property (nonatomic, retain) UILabel *addressLabel;
@property (nonatomic, retain) UILabel *mileageLabel;
@property (nonatomic, retain) UIImageView *bizImage;
@end
非常简单明了。我也在单元格中的 cellForRowAtIndexPath 方法中添加了一个detailDisclosureButton,并且方法 accessoryButtonTappedForRowWithIndexPath:
正在被调用,但 didSelectRowAtIndexPath 没有被调用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellView"
owner:self options:nil];
#ifdef __IPHONE_2_1
cell = (CustomCell *)[nib objectAtIndex:0];
#else
cell = (CustomCell *)[nib objectAtIndex:1];
#endif
}
// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
/*
CODE TO POPULATE INFORMATION IN CUSTOM CELLS HERE
*/
#ifdef __IPHONE_3_0
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
#endif
return cell;
}
我在所有方法和断点中放置了 NSLog。我试图调用的方法不是,但在我的 CustomCell 类中,以下方法是。那么有没有办法让 didSelectRowAtIndexPath 在使用 CustomCell 时被调用?
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
I'm populating a UITableView with CustomCells and I'm trying to get didSelectRowAtIndexPath called. Here is the header for the Custom Cell.
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
IBOutlet UILabel *bizNameLabel;
IBOutlet UILabel *addressLabel;
IBOutlet UILabel *mileageLabel;
IBOutlet UIImageView *bizImage;
}
@property (nonatomic, retain) UILabel *bizNameLabel;
@property (nonatomic, retain) UILabel *addressLabel;
@property (nonatomic, retain) UILabel *mileageLabel;
@property (nonatomic, retain) UIImageView *bizImage;
@end
Pretty simple and straightforward. I have a detailDisclosureButton I'm adding to the cell as well in the cellForRowAtIndexPath method in the cell as well, and the method accessoryButtonTappedForRowWithIndexPath:
is being called, but didSelectRowAtIndexPath is not.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellView"
owner:self options:nil];
#ifdef __IPHONE_2_1
cell = (CustomCell *)[nib objectAtIndex:0];
#else
cell = (CustomCell *)[nib objectAtIndex:1];
#endif
}
// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
/*
CODE TO POPULATE INFORMATION IN CUSTOM CELLS HERE
*/
#ifdef __IPHONE_3_0
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
#endif
return cell;
}
I put an NSLog inside all the methods as well as break points. The method I'm trying to get called is not, but inside my CustomCell class, the following method is. So is there a way to get didSelectRowAtIndexPath to get called while using a CustomCell?
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查您是否为 myTableView 的父视图设置了 UITapGestureRecognizer ..这可能会超越触摸事件并消耗它。
check if you have a UITapGestureRecognizer set for myTableView's parent view ..that is probably over riding the touch event and consuming it.
是否定制单元应该没有任何区别。您处于编辑模式吗?如果是,则必须在 tableView 上设置
allowsSelectionDuringEditing = true
。Custom cell or not should not make any difference. Are you in editing mode? If you are, you have to set
allowsSelectionDuringEditing = true
on the tableView.如果您在 xib 中使用 Tableview ..所以您想在文件所有者中提供 tableview 的数据源和委托连接..
if you are use Tableview in your xib..so u want to give tableview's data source and delegate connection in file owner..
既然您说
tableView:accessoryButtonTappedForRowWithIndexPath:
被调用,我们就知道您的 tableView 的 delegate 属性已正确设置。所以tableView:didSelectRowAtIndexPath:
也应该被调用。如果没有被调用,我最好的猜测是方法签名中的某个地方有拼写错误。将此方法签名复制并粘贴到您的代码中,以确保您没有意外省略“tableView:”部分或出现大小写错误。编辑:次要假设:在定义自定义单元格的 .xib 中,确保选中“用户交互启用”。
Since you say that
tableView:accessoryButtonTappedForRowWithIndexPath:
is getting called, we know that your tableView's delegate property is properly set. SotableView:didSelectRowAtIndexPath:
should be getting called as well. If it isn't getting called, my best guess is that you have a typo somewhere in the method signature. Copy and paste this method signature into your code to make sure you didn't accidentally omit the "tableView:" part or make a capitalization error.EDIT: Secondary hypothesis: In the .xib where you've defined your custom cell, make sure "User Interaction Enabled" is checked.