【问题标题】:Objective-C: How to generate one unique NSInteger from two NSIntegers? [duplicate]Objective-C:如何从两个 NSInteger 生成一个唯一的 NSInteger? [复制]
【发布时间】:2011-06-28 23:05:57
【问题描述】:

可能重复:
Pass two integers as one integer

这可以在 Objective-C 中使用吗? Pass two integers as one integer

如果是这样,我该如何使用NSInteger

我之所以问,是因为我想从 NSUIntegers rowsectionUITableView 中计算唯一的 NSInteger tag

看,我正在处理一个UITableViewController,它有三个部分,每个部分都有多行。每行都有一个UISwitch,每个UISwitch 都链接到相同的目标-操作方法switchAction:

switchAction:,我的计划是检查sendertag以找出UITableViewUISwitchNSIndexPathsection&row)。

所以,我想要两种方法:

+ (NSInteger)integerFromInteger1:(NSInteger)int1 integer2:(NSInteger)int2;
+ (NSIndexPath *)indexPathFromInteger:(NSInteger)integer;

用 C 编写的第一种方法可能效果更好。如果您愿意,也可以使用。

【问题讨论】:

  • 我不知道你为什么要这样使用标签,但你已经链接到另一个回答这个问题的 SO 问题。
  • @Kevin Ballard,好的,你会采取什么方法来确定是哪个交换机发送了switchAction: 消息?
  • 交换机将自己作为第一个参数发送给switchAction:。你已经知道了,因为你说你的计划是检查sendertagsender 是开关)。
  • @Kevin Ballard,对。抱歉,我的意思是how do you figure out the index path of the UISwitch?谢谢你的建议。我想我找到了更好的方法。

标签: objective-c uitableview uiview tags


【解决方案1】:

与其搞乱位移,不如试试这个:

首先,找到包含 UISwitch 的 UITableViewCell。如果您有自定义 UITableViewCell 子类,只需将 UISwitch 的目标/操作指向包含它的单元格上的方法。如果您使用的是库存 UITableViewCell,您可以通过在循环中调用 superview 来找到包含 UISwitch 的 UITableViewCell。

一旦你有了 UITableViewCell,在你的视图控制器上调用一个方法(或者任何可以访问 UITableView 的方法),你就可以调用 UITableView 的indexPathForCell: 方法来获取一个带有节和行的 NSIndexPath 对象。

【讨论】:

    【解决方案2】:

    根据How to convert An NSInteger to an int?NSInteger 在每个系统/架构上始终至少为 32 位,所以是的,Pass two integers as one integer 的答案将起作用。

    【讨论】:

      【解决方案3】:

      基于@benzado's answer,我想出了一个漂亮的解决方案,用于获取发送switchAction:消息的UISwitch的索引路径。

      - (void)switchAction:(id)sender {
          UISwitch *onOff = (UISwitch *)sender;
          NSIndexPath *indexPath = [self.tableView indexPathForCell:
                                    (UITableViewCell *)[onOff superview]];
          // carry on...
      }
      

      不需要标签。穿上裤子就行了。

      【讨论】:

      • 我不确定假设UITableViewCellaccessoryViewsuperviewUITableViewCell 本身是否安全。例如,如果 Apple 决定改变这一点怎么办?我想,正如@benzado 建议的那样,使用循环会更好。或者,如果可以,请使用标签。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 2023-03-31
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多