Reputation: 2158
In landscape view I have a UIScrollView where I add UITableViews dynamically. UITableViews are 200px wide so there are can be 2-3 of them on the screen. Each cell has a button.
When the button is pushed how can I know which UITableView that button belong to?
PS implementation of cellForRowAtIndexPath:
cell = (CellHistory*)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CellHistory" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[CellHistory class]])
{
cell = (CellHistory *)currentObject;
break;
}
}
}
UIButton *noteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[noteButton setFrame:CGRectMake(0, 0, 44, 44)];
[cell addSubview:noteButton];
[noteButton addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
return cell;
PS2 how I add UITableViews to UIScrollView:
for (int i=0; i<allDays.count; i++) {
CGRect landTableRect = CGRectMake(landContentOffset+0.0f, 60.0f, 200.0f, 307.0f);
landTableView = [[UITableView alloc] initWithFrame:landTableRect style:UITableViewStylePlain];
[landTableView setTag:i];
[landTableView setNeedsDisplay];
[landTableView setNeedsLayout];
landTableView.delegate = self;
landTableView.dataSource = self;
[_landScrollView addSubview:landTableView];
[landTableView reloadData];
landContentOffset += landTableView.frame.size.width;
_landScrollView.contentSize = CGSizeMake(landContentOffset, _landScrollView.frame.size.height);
[allLandTableViews addObject:landTableView];
}
Upvotes: 0
Views: 143
Reputation: 556
You can use a helpful category method on UIView
to just walk your view hierarchy until a matching parent is discovered:
@implementation UIView (ParentOfClass)
- (UIView *)parentViewWithClass:(Class)aClass
{
UIView *current = self;
UIView *result = nil;
while ((current = current.superview) != nil) {
if ([current isKindOfClass:aClass]) {
result = current;
break;
}
}
return result;
}
@end
Then in your UIButton
handling code you get the UITableView by a simple call:
- (IBAction)pressedButton:(id)sender
{
UITableView *tableView = [sender parentViewWithClass:[UITableView class]];
// ...
}
Upvotes: 1
Reputation: 4271
In your cellForRowAtIndexPath:
check
if (tableView == youTableViewA)
button.tag = 1;
else if (tableView == yourTableViewB)
button.tag = 2;
. . . And so on.
Where you are assigning a target to your button with addTarget:action:forControlEvents:
make sure your target method accepts a parameter:
- (void) targetMethod:(id)sender{
if (sender.tag == 1){
//tableViewA
}else if (sender.tag == 2){
//tableViewB
}
}
. . . and so on.
Upvotes: 1
Reputation: 9823
You should add a tag to each button that matches the tag for each table view. Then you can compare them
Upvotes: 1