-didSelectRowAtIndexPath:不被调用

本文翻译自:-didSelectRowAtIndexPath: not being called

I'm writing an iOS app with a table view inside a tab view. 我正在写一个带有选项卡视图内的表格视图的iOS应用。 In my UITableViewController , I implemented -tableView:didSelectRowAtIndexPath: , but when I select a row at runtime, the method isn't being called. 在我的UITableViewController ,我实现了-tableView:didSelectRowAtIndexPath:但是当我在运行时选择一行时,不会调用该方法。 The table view is being populated though, so I know that other tableView methods in my controller are being called. 虽然正在填充表视图,所以我知道正在调用控制器中的其他tableView方法。

Does anyone have any ideas what I may have screwed up to make this happen? 有谁知道我可能为实现这一目标而搞砸了什么?


#1楼

参考:https://stackoom.com/question/14zR/didSelectRowAtIndexPath-不被调用


#2楼

All good answers, but there's one more to look out for... 所有好的答案,但还有更多需要注意的地方...

(Particularly when creating a UITableView programmatically) (特别是在以编程方式创建UITableView时)

Make sure the tableView can respond to selection by setting [tableView setAllowsSelection:YES]; 通过设置[tableView setAllowsSelection:YES];来确保tableView可以响应选择[tableView setAllowsSelection:YES]; or removing any line that sets it to NO . 或删除任何将其设置为NO


#3楼

Giving my 2 cents on this. 为此给我2美分。

I had a Custom UITableViewCell and there was a button covering the whole cell, so when the touch happened, the button was selected and not the cell. 我有一个Custom UITableViewCell,并且有一个覆盖整个单元格的按钮,因此当发生触摸时,该按钮而不是该单元格被选中。

Either remove the button or in my case, I set User Interation Enable to false on the button, that way the cell was the one selected. 要么删除按钮,要么就我而言,我将按钮上的User Interation Enable设置为false,这样该单元格即被选中。


#4楼

If you read this, so still doesn't solve the problem. 如果您阅读此内容,那么仍然无法解决问题。

I have custom cell, where checkbox " User Interaction Enabled " was disable. 我有自定义单元格,其中复选框“ User Interaction Enabled ”已禁用。 So, I Just switch on it. 所以,我只是打开它。 Good luck. 祝好运。


#5楼

Just in case someone made the same stupid mistake as I did: 万一有人犯了和我一样的愚蠢错误:

Check out if the method name of what you expect of being didSelect may accidentally be gotten didDeselect in some way. 看看,如果你希望成为什么样的方法名didSelect可能会意外地得到didDeselect以某种方式。 It took about two hours for me to find out ... 我花了大约两个小时才找到...


#6楼

Another thing that might lead to the issue is not selected selection kind: 可能导致问题的另一件事不是选定的选择类型:

UITableView选择种类

Should be Single Selection for normal selection, should not be No Selection . 应该是Single Selection正常的选择, 应该是No Selection

To do this programmatically, do: 要以编程方式执行此操作,请执行以下操作:

tableView.allowsSelection = YES
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过使用 UITableView 来实现一个日历表格。具体实现步骤如下: 1. 创建一个继承自 UIViewController 的类,例如名为 `CalendarViewController`。 2. 在 `CalendarViewController` 中创建一个 UITableView,并将其添加到当前视图中。 3. 实现 UITableViewDataSource 和 UITableViewDelegate 协议中的方法,以便为 UITableView 提供数据和响应用户的操作。 4. 在 UITableView 中创建 UITableViewCell,并为其添加子视图,例如 UILabel,来显示日期等信息。 5. 根据需要,可以使用 NSDate 和 NSCalendar 等类来计算日期和星期等信息。 6. 在 UIViewController 的 viewDidLoad 方法中,设置 UITableView 的数据源和代理为当前的 `CalendarViewController` 对象。 下面是一个简单的示例代码,可以根据需要进行修改和扩展: ``` // CalendarViewController.h #import <UIKit/UIKit.h> @interface CalendarViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @end // CalendarViewController.m #import "CalendarViewController.h" @interface CalendarViewController () @end @implementation CalendarViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建 UITableView self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.delegate = self; [self.view addSubview:self.tableView]; } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 返回日历表格的总共行数 return 6; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回每一行日历表格的列数 return 7; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CalendarCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // 计算当前单元格对应的日期 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.day = indexPath.row + indexPath.section * 7; NSDate *date = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0]; // 显示日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"d"; cell.textLabel.text = [formatter stringFromDate:date]; return cell; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } @end ``` 在使用时,可以创建一个 `CalendarViewController` 对象,并将其添加到当前视图中即可: ``` CalendarViewController *calendarViewController = [[CalendarViewController alloc] init]; [self addChildViewController:calendarViewController]; [self.view addSubview:calendarViewController.tableView]; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值