简体   繁体   English

如何检测何时未选择UITableView表格单元格?

[英]How do I detect when no UITableView table cell has been selected?

I have a start date/end date feature in my iPhone application. 我的iPhone应用程序中有开始日期/结束日期功能。 It is similar to that in the iPhone's native Calendar application, except there's no "All-day" option. 它与iPhone的本机Calendar应用程序类似,不同之处在于没有“全天”选项。 That is, all the user can do is enter a start time/date and an end time/date. 也就是说,用户所能做的就是输入开始时间/日期和结束时间/日期。

The interface is a simple grouped UITableView with two cells. 该接口是具有两个单元格的简单分组的UITableView。 The user selects either the Start cell or the End cell (turning it blue), and then uses a UIDatePicker to change the values for each. 用户选择“开始”单元格或“结束”单元格(将其变为蓝色),然后使用UIDatePicker更改每个单元格的值。

I've got everything working as expected, except that the user can click outside the table, which causes none of the table cells to be selected. 除了用户可以在表外部单击之外,其他所有工作均按预期进行,这不会导致选择任何表格单元格。 The problem is, the user is still editing the previously-selected cell. 问题是,用户仍在编辑先前选择的单元格。

Ideally I would be able to restrict the user to always have one of the table cells selected. 理想情况下,我将能够限制用户始终选择一个表单元格。 Barring that, I would settle for being able to detect that no cells have been selected, so I can intercept the UIDatePicker before it edits anything. 除非如此,否则我将能够检测到未选择任何单元格,因此可以在UIDatePicker编辑任何内容之前对其进行拦截。

You could implement the willDeselectRowAtIndexPath from the UITableViewDelegate . 您可以从UITableViewDelegate实现willDeselectRowAtIndexPath Return nil and the row won't be deselected: 返回nil并且不会取消选择该行:

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

Or, you could implement the didDeselectRowAtIndexPath and you would know that it was unselected. 或者,您可以实现didDeselectRowAtIndexPath并且您会知道它未被选中。 But you'll have to store it in a member variable or somewhere: 但是您必须将其存储在成员变量中或某个地方:

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath 

It appears, from your question, that you have a tableView with a UIPicker below it, in the same view. 从您的问题来看,在同一视图中似乎有一个tableView及其下面的UIPicker。 You may want to put the picker in a separate view that you push onto the screen when the user selects one of the cells. 您可能希望将选择器放在一个单独的视图中,当用户选择一个单元格时,将其推到屏幕上。 Using this technique the user would definitely have to choose one of the cells to edit information in the picker. 使用这种技术,用户肯定将必须选择一个单元以编辑选择器中的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM