I am aware that there are many similar questions here, but none solved my issue. There are many sections in my UITableView and when I click / checkmark some buttons in the cells and scroll down, the check mark disappears when scrolled up again. Here's what I coded..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"CheckBoxedCell";
// NSString *cellId = [NSString stringWithFormat:#"Section:%d Row:%d",indexPath.section,indexPath.row];
CheckBoxedCellClass *cell = (CheckBoxedCellClass *)[self.tableViewContact dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
NSArray *nib;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"CheckBoxedCellClass" owner:self options:nil];
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"CheckBoxedCellClass_iPad" owner:self options:nil];
}
for (id object in nib)
{
if([object isKindOfClass:[CheckBoxedCellClass class]])
{
cell = (CheckBoxedCellClass *)object;
break;
}
}
cell = [nib objectAtIndex:0];
}
SaveCheckBoxedView *saveContact;
if(isFiltered == YES)
{
saveContact = [filterdArray objectAtIndex:indexPath.row];
cell.nameLabel.text = saveContact.nameString;
}
else
{
saveContact = [mutableArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
}
cell.companyLabel.text = saveContact.companyString;
cell.invIdLabel.text = [NSString stringWithFormat:#"%#", saveContact.invitId];
//set fonts
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
[cell.companyLabel setFont:[UIFont italicSystemFontOfSize:10.0]];
}
else
{
[cell.companyLabel setFont:[UIFont italicSystemFontOfSize:14.0]];
}
//handling check box
NSInteger rowNumber = 0;
for(NSInteger i = 0; i < indexPath.section ; i++)
{
rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i];
}
rowNumber += indexPath.row;
UIButton *checkBox;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)];
}
else
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)];
}
[checkBox setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:#selector(checkBoxClicked:event:) forControlEvents:UIControlEventTouchUpInside];
checkBox.tag = rowNumber;
[cell.contentView addSubview:checkBox];
// handle check box view reset when scrolled
for(NSIndexPath *path in checkedIndexPaths)
{
if(path.section == indexPath.section && path.row == indexPath.row)
{
if(!checkedIndexPaths)
{
checkedIndexPaths = [[NSMutableSet alloc] init];
[checkedIndexPaths addObject:[NSNumber numberWithInt:rowNumber]];
}
}
}
return cell;
}
-(void)checkBoxClicked:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableViewContact];
NSIndexPath *indexPath = [self.tableViewContact indexPathForRowAtPoint: currentTouchPosition];
NSLog(#"value of indexPath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);
UIButton *tappedButton = (UIButton*)sender;
NSLog(#"Tag number = %d", [sender tag]);
if([tappedButton.currentImage isEqual:[UIImage imageNamed:#"checkBox.png"]])
{
[sender setImage:[UIImage imageNamed: #"checkBoxMarked.png"] forState:UIControlStateNormal];
if(isFiltered == YES)
{
NSString *addId = [filteredArrayOfIds objectAtIndex:indexPath.row];
NSLog(#"filterd id = %#", addId); //get filtered array here
[arrayOfIds addObject:addId];
}
else
{
NSString *finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];
NSLog(#"Tagged checked button id = %#", finalIntId);
[arrayOfIds addObject:finalIntId];
}
}
else
{
[sender setImage:[UIImage imageNamed:#"checkBox.png"]forState:UIControlStateNormal];
NSLog(#"UnChecked");
[arrayOfIds removeObjectAtIndex:tappedButton.tag];
}
}
If I am not mistaken, you are never setting checkBox.checked?
When you scroll up and down only the visible cells will be dequeued and configured. So whenever you scroll down a whole page and then up again the cells will be recreated (you can NSLog at the entry point of cellForRowAtIndexPath to see what I mean).
In this method you recreate the button. Remember that table view cells are cached so if you created a cell once it will probably be used again when needed so create the button only if the cell is created for the first time and not on every call to cellForRowAtIndexPath.
If you use a custom cell you can add a boolean member to indicate that the cell was already created and there is no need to add another newly created button which will hide you original one when you add it with [cell.contentView addSubview:checkBox]; (or better yet do the button creation in this custom cell's init method which will be more easier and correct)
I used to implement similar things with button. So inside the cell, you define a function which changes the background of the checkbox, or the status of the checkbox based on the model.
-(void) setModel:(Model *)model {
[self.button setSomeStyleBasedOnYourModel];
}
If you have that checkbox in your tableview of your storyboard, you can linked a target from the storyboard to you controller.
- (IBAction)doTaskButtonPressed:(UIButton *)sender {
[super doTaskButtonPressed:sender];
}
Related
I am creating a editable table view where user can enter its values into tableview cells. Table view custom cells contains multiple textview. I have a button like Edit/Done. On click of edit, user should be able to enter values in table view cells containing textview. On Done click, it will disable editable property.
below is my method of Edit/Delete button, but its not functioning as I am wishing to, what will be the possible solution ?
- (IBAction) toggleEdit:(id)sender {
[self.tableview setEditing:!self.tableview.editing animated:YES];
static NSString *simpleTableIdentifier = #"ScoreCustomCell";
ScoreCustomCell *cell = (ScoreCustomCell *)[self.tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ScoreCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.tableview.editing) {
NSString *tempStr=[NSString stringWithFormat:#"Done"];
[sender setTitle:tempStr forState:UIControlStateNormal];
for (int i=0; i<=23; i++) {
if (cell.scoreText.tag==i) {
[self.tableview setUserInteractionEnabled:YES];
[cell setUserInteractionEnabled:YES];
cell.scoreText.editable=YES;
[cell.scoreText setUserInteractionEnabled:YES];
[cell.scoreText becomeFirstResponder];
[self.tableview reloadInputViews];
}
}
}
else {
NSString *tempStr2=[NSString stringWithFormat:#"Edit"];
[sender setTitle:tempStr2 forState:UIControlStateNormal];
for (int i=0; i<=23; i++) {
if (cell.scoreText.tag==i) {
cell.scoreText.editable=NO;
[self.tableview reloadInputViews];
} }
}
}
Update your toggleEdit method like this:
- (IBAction) toggleEdit:(UIButton *)sender {
[self.tableview setEditing:!self.tableview.editing animated:YES];
if (self.tableview.editing) {
NSString *tempStr = [NSString stringWithFormat:#"Done"];
[sender setTitle:tempStr forState:UIControlStateNormal];
}
else {
NSString *tempStr2=[NSString stringWithFormat:#"Edit"];
}
[sender setTitle:tempStr2 forState:UIControlStateNormal];
[self.tableView reloadData];
}
Now in cellForRowAtIndexPath method at the end put an if-else
cellForRowAtIndexPath
{
...
... Your code here ...
...
// before returning cell add this two lines
[cell.scoreText setEditable:self.tableview.editing];
[cell.scoreText setUserInteractionEnabled:self.tableview.editing];
return cell;
}
Hope this helps.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"CustomCell";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner: self options: nil ];
cell = [nib objectAtIndex:0];
}
//cell.lblName.text = [items objectAtIndex:indexPath.row];
NSMutableDictionary *dic = [items objectAtIndex:indexPath.row];
cell.imgface.image = [dic objectForKey:#"Image"];
cell.imgface.layer.cornerRadius = cell.imgface.frame.size.width / 2;
cell.imgface.layer.borderWidth = 3.0f;
cell.imgface.layer.borderColor = [UIColor whiteColor].CGColor;
cell.imgface.clipsToBounds = YES;
cell.lblName.text = [dic objectForKey:#"Name"];
[cell.btnPhone setTitle:[dic objectForKey:#"Phone"] forState:(UIControlStateNormal)];
cell.btnPhone.tag = indexPath.row;
cell.btnstar.tag = indexPath.row;
[cell.btnPhone addTarget:self action:#selector(dial:) forControlEvents:(UIControlEventTouchUpInside)];
[cell.btnstar addTarget:self action:#selector(toggleimage:) forControlEvents:UIControlEventTouchUpInside];
if (indexPath.section == 0)
{
star = [UIImage imageNamed:#"Star-Favorites.png"];
[cell.btnstar setBackgroundImage:star forState:UIControlStateNormal];
}
else
{
star = [UIImage imageNamed:#"keditbookmarks.png"];
[cell.btnstar setBackgroundImage:star forState:UIControlStateNormal];
}
return cell;
}
write below code in button IBAction and you will get indexpath.row and indexpath.section
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
NSLog(#"selected tableview row is %d",indexPath.row);
You have some different ways to do that:
• When you configure the cell set the tag of the cell or the button as the row and retrieve that on the IBAction (I hate tags though)
• Define a delegate for the table cell and set it when you configure it. Then call the delegate method passing the cell and find the relative index (-indexPathForCell:)
• The same as before but pass the cell through a notification instead of delegates
first of all on, button IBAction call this method
- (IBAction)btnClick:(UIButton)sender
{
UITableViewCell *cellOfcontrol=[self cellWithSubview:sender];
NSIndexPath *indexPath = [self.tableview indexPathForCell:cellOfcontrol];
}
cellWithSubview function
- (UITableViewCell *)cellWithSubview:(UIView *)subview
{
while (subview && ![subview isKindOfClass:[UITableViewCell self]])
subview = subview.superview;
return (UITableViewCell *)subview;
}
I have this collapsable UITableView where there is a UITextField and a UIButton in the last cell in each table section. I would like to send the text in the UITextField to the function that is called by the UIButton that is next to it in the same cell, but I am baffled in how to achieve this. Thanks in advance for the help!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self tableView:_tableView canCollapseSection:indexPath.section])
{
int num = 1;
if (self.chat[indexPath.section - 1][#"num"] != nil)
num = [self.chat[indexPath.section - 1][#"num"] intValue] + 1;
if (!indexPath.row)
{
cell.textLabel.text = self.chat[indexPath.section - 1][#"msg"]; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else if (indexPath.row < num && indexPath.row >= 1)
{
cell.textLabel.text = self.chat[indexPath.section - 1][key];
cell.accessoryView = nil;
}
else
{
///////////////////////////////////////////
////////This is the important part/////////
///////////////////////////////////////////
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];
self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
[self.sendButton setTitle:#"Reply" forState:UIControlStateNormal];
[self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
[cell addSubview:self.sendButton];
[self.sendButton addTarget:self action:#selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:field];
cell.accessoryView = nil;
}
}
else
{
cell.accessoryView = nil;
cell.textLabel.text = #"Normal Cell";
}
return cell;
}
Give some unique tag to textfield and button by below way:
///////////////////////////////////////////
////////This is the important part/////////
///////////////////////////////////////////
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];
self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
[self.sendButton setTitle:#"Reply" forState:UIControlStateNormal];
[self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
self.sendButton.tag = indexPath.section *1000 + indexPath.row;
[cell addSubview:self.sendButton];
[self.sendButton addTarget:self action:#selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];
field.tag = self.sendButton.tag + 1;
[cell addSubview:field];
cell.accessoryView = nil;
Now on button event,
-(void) sendReply:(UIButton *)sender
{
UITextField *field = [YOOUR_TABLE_VIEW viewWithTag:sender.tag + 1];
//Do you coding here
}
Make a Custom UITableViewCell that has uitextfield and a button on it and make a protocol/delegate of that custom uitableviewcell you've created.. so you can have more control of your code and the event of your button in the future..
check this tutorial: http://www.codigator.com/tutorials/ios-uitableview-tutorial-custom-cell-and-delegates/
cheers
For this,
In cellForRowAtIndexPath: method where you are allocating the send button add one line just to give some identifier to send button as below,
[self.sendButton setAccessibilityIdentifier:[NSString stringWithFormat:#"%d#%d",indexPath.row,indexPath.section]];
Now, in sendReply: method,
//First get the row and section information
NSString *str=[sender accessibilityIdentifier];
NSArray *arr=[str componentsSeparatedByString:#"#"];
//Get the index path
NSIndexPath *iRowId =[NSIndexPath indexPathForRow:[[arr objectAtIndex:0] intValue] inSection:[arr objectAtIndex:1] intValue]];
//get the cell
UITableViewCell *objCell=(UITableViewCell*)[tblviwBasketCell cellForRowAtIndexPath:iRowId];
Now objCell will be the cell in which you have added the button and text view. so get the subviews of objCell and access the textfield to get the text.
As you say, there is a text field in each section. You should set the tag of your UIButton and UITextField same as indexPath.section.
Then, in the target method, use this tag value to get the relevant cell from the relevant section, and iterate over it's subviews to get your textField.
In the target method, you should do something like this:
- (void) sendReply:(id)sender {
int section = [(UIButton*)sender tag];
int row = [myTableView numberOfRowsInSection:section] - 1;//assuming it is the last cell in the section that contains your textField.
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* cell = [myTableView cellForRowAtIndexPath:indexPath];
for (UIView* vu in cell.subviews) {
if ([vu isKindOfClass:[UITextField class]]) {
NSString* textString = [(UITextField*)vu text];
//perform any tasks you want with the text now
}
}
}
You can simply use viewWithTag:, since it does an iterative search, but all the extra code is to avoid iterating over all the cells before reaching your relevant cell.
I know that there are many duplicates here regarding this question but my requirement is that I added 2 UIButtons on one cell and both the buttons will open two different views. If I set the property userInteractionEnabled to YES, then it will not pic the 'finalID' from didSelectRowAtIndexPath from the below code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.tableViewProject){
static NSString *cellId = #"attachmentCellId";
attachmentCell *cell = (attachmentCell *)[self.tableViewProject dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"attachmentCell" owner:self options:Nil];
for(id object in nib)
{
if([object isKindOfClass:[attachmentCell class]])
{
cell = (attachmentCell *)object;
break;
}
}
UIButton *button;
button = [[UIButton alloc] initWithFrame:CGRectMake(162, 0, 75, 53)];
[button addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
button.userInteractionEnabled = YES;
//button.userInteractionEnabled = NO;
[cell.contentView addSubview:button];
UIButton *buttonAttach = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 75, 53)];
[buttonAttach addTarget:self action:#selector(buttonAttachClicked) forControlEvents:UIControlEventTouchUpInside];
buttonAttach.userInteractionEnabled = YES;
//buttonAttach.userInteractionEnabled = NO;
[cell.contentView addSubview:buttonAttach];
cell = [nib objectAtIndex:0];
SaveAttachment *attach = [array objectAtIndex:indexPath.row];
cell.name.text = attach.name;
cell.list.text = [NSString stringWithFormat:#"%d", attach.list];
cell.attachment.text = [NSString stringWithFormat:#"%d", attach.attachment];
cell.date.text = attach.date;
}
return cell;
}
And my DidSelectRowAtIndexPath is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Array == %#", anotherTempArray);
NSString *finalId = [NSString stringWithFormat:#"%#", [anotherTempArray objectAtIndex:indexPath.row]];
NSLog(#"final id for selected row = %#", finalId);
NSUserDefaults *defaultForFinalId = [NSUserDefaults standardUserDefaults];
NSString *setFinalId = finalId;
[defaultForFinalId setObject:setFinalId forKey:#"SETFINALID"];
if(tableView == self.tableViewProject)
{
[self buttonClicked];
//[self viewDidLoadForList];
}
if(tableView == self.tableViewAttachmentList)
{
[self buttonAttachClicked];
}
}
If you want to call the selector of UIButton inside cell, then what you dont need to use didSelectRowAtIndexPath method.
What you did was correct upto adding handler to UIButton. Now, remove your didSelectRowAtIndexPath code to button's click handler. Here is how you can get indexPath from the button click handler.
- (void)buttonClicked:(UIButton *)sender {
UITableViewCell *cell = (UITableViewCell*)sender.superview.superview; //Since you are adding to cell.contentView, navigate two levels to get cell object
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// Now you can do the code you put in didSelectRow here.
}
Hope this helps.
change your code for button selector
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; //add colon in selector buttonClicked
[buttonAttach addTarget:self action:#selector(buttonAttachClicked:) forControlEvents:UIControlEventTouchUpInside]; //add colon in selector buttonAttachClicked
If U use buttons than U don't need to use didSelectRowAtIndexPath. Set tags on buttons to match indexPath.row of the cell in cellForRowAtIndexPath... This way when U press the button in buttonAttachClicked or buttonClicked just check the senders tag and pull that cell from the UITableView
Use this code for cellForRowAtIndexPath
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.tableViewProject){
static NSString *cellId = #"attachmentCellId";
attachmentCell *cell = (attachmentCell *)[self.tableViewProject dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"attachmentCell" owner:self options:Nil];
for(id object in nib)
{
if([object isKindOfClass:[attachmentCell class]])
{
cell = (attachmentCell *)object;
break;
}
}
cell.button = [[UIButton alloc] initWithFrame:CGRectMake(162, 0, 75, 53)];
[cell.button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.button];
cell.buttonAttach = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 75, 53)];
[cell.buttonAttach addTarget:self action:#selector(buttonAttachClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.buttonAttach];
cell = [nib objectAtIndex:0];
}
SaveAttachment *attach = [array objectAtIndex:indexPath.row];
cell.name.text = attach.name;
cell.list.text = [NSString stringWithFormat:#"%d", attach.list];
cell.attachment.text = [NSString stringWithFormat:#"%d", attach.attachment];
cell.date.text = attach.date;
[cell.button setTag:indexPath.row];
[cell.buttonAttach setTag:indexPath.row];
return cell;
}
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];
}
You should add a tag to each button that matches the tag for each table view. Then you can compare them
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.
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]];
// ...
}