Reputation: 5108
I have a tableview with custom tableview cell. in the tableview cell there are two labels and one button.what I want it to fire the button action for user selected row to hide a label in the same row.
this is my controller for table view
ViweController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tablev;
@end
ViewController.m
#import "ViewController.h"
#import "TestTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
cell.selectionStyle = UITableViewCellFocusStyleCustom;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger sec = indexPath.section;
NSInteger rw = indexPath.row;
TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
cell.numberlabel.hidden = YES;
NSLog(@"selected section :%li ---> selected row :%li",(long)sec, (long)rw);
//in here I want fire the button acction in the cell for each row when cell tap.(not when the button click in the cell).
}
TestTableViewCell.h
#import <UIKit/UIKit.h>
@interface TestTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *staticlabel;
@property (weak, nonatomic) IBOutlet UILabel *numberlabel;
@property (weak, nonatomic) IBOutlet UIButton *hidebutton;
@end
TestTableViewCell.m //I have tried to implement button click method here.It worked.but at that point it didn't recognised which cell is taped.
**NOTE : I have tried to implement button click method here.I worked.but at that point it didn't recognised which cell is taped. **
Upvotes: 0
Views: 1635
Reputation: 438
you need to get the correct cell first , You can achieve it by replacing
TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
by this:
TestTableViewCell *cell = (TestTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
Your method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger sec = indexPath.section;
NSInteger rw = indexPath.row;
TestTableViewCell *cell = (TestTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.numberlabel.hidden = YES;
NSLog(@"selected section :%li ---> selected row :%li",(long)sec, (long)rw);
//in here I want fire the button acction in the cell for each row when cell tap.(not when the button click in the cell).
}
Hope this Helps!
Upvotes: 0
Reputation: 3219
You can get indexPath.row
of UITableView
in button action:
Make action of button in yourviewcontroller.h
file:
- (IBAction) My_button:(id)sender;
In yourviewcontroller.m
file:
- (IBAction)My_button:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero
toView:self.tbl_view];
NSIndexPath *indexPath = [self.tbl_view indexPathForRowAtPoint:buttonPosition];
NSLog(@"%ld",(long)indexPath.row);
}
And If you want to do this in your didSelectRowAtIndexPath
then you don't need to dequeue cell again.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",(long)indexPath.row);
NSLog(@"%ld",(long)indexPath.section);
TestTableViewCell *cell = (TestTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.numberlabel.hidden = YES;
}
Upvotes: 1
Reputation: 49730
You can implement by two way one you can add Button Action in to your cellForRowAtIndexPath
and setting tag of Button like following code:
hidebutton.tag=indexPath.row;
[hidebutton addTarget:self
action:@selector(hideaction:)
forControlEvents:UIControlEventTouchUpInside];
Its Action Method is
-(IBAction)hideaction:(UIButton*)sender
{
NSIndexPath *hideIndexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
TestTableViewCell *cell = (TestTableViewCell *)[self.tablev cellForRowAtIndexPath:hideIndexpath];
}
Another way is you can achieve this same from DidSelect method with following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TestTableViewCell *cell = (TestTableViewCell *)[self.tablev cellForRowAtIndexPath:indexPath];
//use your cell object for hide anyting
}
Upvotes: 4