Objective-C: How to generate one unique NSInteger from two NSIntegers?

Possible duplicate:
Pass two integers as one integer

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

If so, how to do it with NSInteger ?

I ask because I want to calculate a unique NSInteger tag from NSUInteger row and section UITableView ?

See, I am dealing with a UITableViewController that has three sections, each of which has multiple rows. Each row has a UISwitch , and each UISwitch is associated with the same target action method, switchAction:

In switchAction: my plan is to check the sender tag to find out the UISwitch NSIndexPath ( section and row ) UITableView .

So I need two methods:

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

The first method may work better written in C. This also works if you prefer.

0
source share
3 answers

Based on the @benzado answer , I came up with a nice solution on how to get the UISwitch index path 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 required. Just hold your pants.

0
source

Instead of messing around with a bit shift, try the following:

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

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

+2
source

According to How to convert NSInteger to int? , NSInteger will always be at least 32 bits in each system / architecture, so yes, answers to Skip two integers as an integer .

0
source

Source: https://habr.com/ru/post/952697/


All Articles