0

Possible Duplicate:
Pass two integers as one integer

Will this work in Objective-C? Pass two integers as one integer

If so, how do I do it with NSInteger?

I'm asking because I want to calculate a unique NSInteger tag from the NSUIntegers row & section of a UITableView?

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:.

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;

The first method may work better written in C. That works too if you prefer.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 2
    I have no idea why you want to use tag this way, but you've already linked to another SO question that answers this one. – Lily Ballard Jun 28 '11 at 23:11
  • @Kevin Ballard, OK, well what approach would you take so that you can figure out which switch sent the `switchAction:` message? – ma11hew28 Jun 29 '11 at 00:11
  • The switch sends itself as the first argument to `switchAction:`. You already know that, since you stated that your plan was to inspect the `sender`'s `tag` (where the `sender` is the switch). – Lily Ballard Jun 29 '11 at 00:25
  • @Kevin Ballard, right. Sorry, what I meant was [how do you figure out the index path of the `UISwitch`](http://stackoverflow.com/questions/6514112/objective-c-how-to-generate-one-unique-nsinteger-from-two-nsintegers/6515240#6515240)? Thanks for your suggestion. I think I found a better approach. – ma11hew28 Jun 29 '11 at 02:49

3 Answers3

2

Rather than messing around with bit-shifting, try this:

First, find the UITableViewCell containing the UISwitch. If you have a custom UITableViewCell subclass, just direct the UISwitch's target/action to a method on the cell that contains it. If you are using a stock UITableViewCell, you could find the UITableViewCell containing the UISwitch by calling superview in a loop.

Once you have the UITableViewCell, call a method on your view controller (or whatever has access to the UITableView) and you can call UITableView's indexPathForCell: method to get an NSIndexPath object with the section and row.

benzado
  • 82,288
  • 22
  • 110
  • 138
0

According to How to convert An NSInteger to an int?, NSInteger will always be at least 32 bits on every system/architecture, so yes, the answers to Pass two integers as one integer will work.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
0

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.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • I'm not sure if it's safe to assume that the `superview` of a `UITableViewCell`'s `accessoryView` is the `UITableViewCell` itself. E.g., what if Apple decided to change this? I guess, as @benzado suggests, using a loop would be better. Or, just use a tag if you can. – ma11hew28 Nov 27 '12 at 17:51