Objective-C:如何从两个 NSInteger 生成一个唯一的 NSInteger? [英] Objective-C: How to generate one unique NSInteger from two NSIntegers?
问题描述
可能的重复:
将两个整数作为一个整数传递
这会在 Objective-C 中工作吗?将两个整数作为一个整数传递
Will this work in Objective-C? Pass two integers as one integer
如果是这样,我该如何使用 NSInteger
来实现?
If so, how do I do it with NSInteger
?
我之所以这么问是因为我想从 NSUInteger
的 row
& 计算一个唯一的 NSInteger 标签
UITableView
的section
?
I'm asking because I want to calculate a unique NSInteger tag
from the NSUInteger
s row
& section
of a UITableView
?
看,我正在处理一个 UITableViewController
,它包含三个部分,每个部分都有多行.每行都有一个 UISwitch
,每个 UISwitch
都链接到相同的目标操作方法,switchAction:
.
See, I'm dealing with a UITableViewController
that has three sections, which each have multiple rows. Every row has a UISwitch
, and each UISwitch
is linked to the same target-action method, switchAction:
.
在switchAction:
中,我的计划是检查sender
的tag
以找出UISwitch
UITableView
的 NSIndexPath
(section
& row
).
In switchAction:
, my plan is to inspect the sender
's tag
to figure out the UISwitch
's NSIndexPath
(section
& row
) of the UITableView
.
所以,我想要两种方法:
So, I want two methods like:
+ (NSInteger)integerFromInteger1:(NSInteger)int1 integer2:(NSInteger)int2;
+ (NSIndexPath *)indexPathFromInteger:(NSInteger)integer;
第一种方法用 C 编写可能效果更好.如果您愿意,也可以.
The first method may work better written in C. That works too if you prefer.
推荐答案
基于 @benzado 的答案,我想出了一个很好的解决方案,用于获取发送 UISwitch
的索引路径>switchAction: 消息.
Based on @benzado's answer, I came up with a beautiful solution for how to get the index path of the UISwitch
that sent the switchAction:
message.
- (void)switchAction:(id)sender {
UISwitch *onOff = (UISwitch *)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:
(UITableViewCell *)[onOff superview]];
// carry on...
}
不需要标签.保持你的裤子.
No tags necessary. Just keep your pants on.
这篇关于Objective-C:如何从两个 NSInteger 生成一个唯一的 NSInteger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!