【问题标题】:UITableView didSelectRowAtIndexPath not being called after programatically selecting row以编程方式选择行后未调用 UITableView didSelectRowAtIndexPath
【发布时间】:2013-10-20 21:34:00
【问题描述】:

在我的应用程序执行期间的不同时间点,需要以编程方式使用 UITableView 选择一行

[self.tableView selectRowAtIndexPath:selectedCellIndexPath animated:false scrollPosition:UITableViewScrollPositionMiddle];

这工作正常,该行在后端突出显示并标记为选中。该表格被配置为允许多选。

但是,如果用户随后取消选择该行,则不会调用“didSelectRowAtIndexPath”函数。奇怪的是,如果他们重新选择它,该函数会按预期再次调用。

知道可能出了什么问题吗?选择行时是否需要重置其他属性或其他内容?

【问题讨论】:

    标签: ios objective-c uitableview didselectrowatindexpath


    【解决方案1】:

    实现UITableViewDelegate协议 .h:

    @interface YourView : UIView <UITableViewDataSource,UITableViewDelegate>
    

    然后在.m:

    self.tableView.delegate = self;
    

    然后写:

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath[
    }
    

    你的方法会被调用

    【讨论】:

      【解决方案2】:

      如果用户随后取消选择该行,则将调用关联的UITableViewDelegate 方法:

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

      【讨论】:

      • 谢谢!一整天的编码工作最终让我无法直截了当地思考。这显然是我的问题的答案,被选为第一名。
      • 我们都去过那里!谢谢伊恩
      【解决方案3】:

      UITableViewDelegate中有两种选择/取消选择行的方法:

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

      【讨论】:

        【解决方案4】:

        一般来说,如果您从代码触发操作,则不应期望调用委托方法。它们只会由用户交互触发。如果您以编程方式将单元格设置为选中,那么取消选中的用户交互很自然会触发关联的委托方法。

        【讨论】:

          猜你喜欢
          • 2012-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-15
          • 1970-01-01
          • 1970-01-01
          • 2015-09-23
          • 2015-10-05
          相关资源
          最近更新 更多