在 tableView 上使用 CustomCell,如何调用 didSelectRowAtIndexPath?

发布于 2024-11-19 22:13:28 字数 1886 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我纯我任性 2024-11-26 22:13:29

检查您是否为 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.

面犯桃花 2024-11-26 22:13:28

是否定制单元应该没有任何区别。您处于编辑模式吗?如果是,则必须在 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.

愁杀 2024-11-26 22:13:28

如果您在 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..

爱殇璃 2024-11-26 22:13:28

既然您说 tableView:accessoryButtonTappedForRowWithIndexPath: 被调用,我们就知道您的 tableView 的 delegate 属性已正确设置。所以 tableView:didSelectRowAtIndexPath: 也应该被调用。如果没有被调用,我最好的猜测是方法签名中的某个地方有拼写错误。将此方法签名复制并粘贴到您的代码中,以确保您没有意外省略“tableView:”部分或出现大小写错误。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

编辑:次要假设:在定义自定义单元格的 .xib 中,确保选中“用户交互启用”。

Since you say that tableView:accessoryButtonTappedForRowWithIndexPath: is getting called, we know that your tableView's delegate property is properly set. So tableView: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.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

EDIT: Secondary hypothesis: In the .xib where you've defined your custom cell, make sure "User Interaction Enabled" is checked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文