Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cstromme

macrumors regular
Original poster
Feb 26, 2007
162
0
So I have a UITableView here with several sections and with several rows per section. Both the number of sections and the number of rows per section is different from each specific case the tableview is used for.

I have two UIButtons in the tableview that both fire their separate methods.

The problem is that I'm having trouble figuring out which section and which row the button belongs to in the method that gets fired when you push the button. After some thinking I realized I could set the row as the tag of the button (since this gets passed into the method I'm running), but that leaves the section undetermined. I then thought perhaps I could set the title of the button to the section, since I'm using an image on the button, but for some reason I can't seem to extract this in the method that gets fired.

Buttons:
Code:
			UIButton *increaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
				NSString *section = [NSString stringWithFormat:@"Section: %i",(indexPath.section - 2)];
				int line = ((indexPath.row - 1) / 2);
				[increaseButton setTag:line];
				[increaseButton setTitle:section
						     forState:UIControlStateNormal];
				[increaseButton setTitleColor:[UIColor greenColor]
							    forState:UIControlStateNormal];
				[increaseButton setImage:[UIImage imageNamed:@"bidincrease.png"]
						        forState:UIControlStateNormal];
				[increaseButton setFrame:CGRectMake(10, 10, 30, 25)];
				[increaseButton setUserInteractionEnabled:YES];
				[increaseButton addTarget:self
						           action:@selector(increaseLine:)
					     forControlEvents:UIControlEventTouchUpInside];
				[cell setAccessoryView:increaseButton];
				
				UIButton *decreaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
				[decreaseButton setTag:line];
				[decreaseButton setTitle:section
						      forState:UIControlStateNormal];
				[decreaseButton setImage:[UIImage imageNamed:@"biddecrease.png"]
							forState:UIControlStateNormal];
				[decreaseButton setFrame:CGRectMake(10, 10, 30, 25)];
				[decreaseButton setUserInteractionEnabled:YES];
				[decreaseButton addTarget:self
							    action:@selector(decreaseLine:)
					      forControlEvents:UIControlEventTouchUpInside];
				[cell.contentView addSubview:decreaseButton];

The increaseLine method looks like this:
Code:
- (void)increaseLine:(UIButton *)sender
{
	NSLog(@"Increasing line %@",sender);
//	NSLog("Section: %@\nLine: %i",[sender currentTitle],[sender tag]);
}

The sender here comes out as:
Increasing line <UIButton: 0x3973d60; frame = (271 10; 30 25); opaque = NO; tag = 2; layer = <CALayer: 0x3999710>>

With no mention of the title. Trying to run that second line that is commented out the app crashes and there's a warning in XCode that I'm passing argument 1 from an incompatible pointer type.

Help!
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
If you have access to the UIButton, and it is a subview of your tableview cell's content view, then you should be able to go up the view hierarchy using the superview method until you find your cell.

You should then be able to get the index path of that cell by using the method [tableView indexPathForCell:yourcell].
 

teek

macrumors member
Feb 12, 2008
88
0
Norway
Use the UIButton's superview

in your IBAction method:

UIButton *theButton = (UIButton*)sender;

UITableViewCell *cell = [theButton superview];

Then to get the row/section use UITableView:indexPathForCell method
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
There are several choices here.

Even though a tag is the size of a single int you can stuff two shorts into it. As long as your row and section count will never be greater than MAX_UNSIGNED_SHORT then this will be simple and will work. Just set the tag for the buttons in your cellForRowAtIndexPath so it contains both row and section.

You can subclass UIButton and add a property for an indexPath or add properties for row and section.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.