2016-07-06 1 views
0

J'ai une vue de table avec une cellule tableview personnalisée. dans la cellule tableview il y a deux étiquettes et un bouton. ce que je veux qu'il déclenche l'action de bouton pour la ligne sélectionnée par l'utilisateur pour cacher une étiquette dans la même rangée.comment déclencher une méthode d'action de bouton dans didselectrowindindpath dans iOS, objectif c

c'est mon contrôleur pour une vue de table

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 // J'ai essayé de mettre en oeuvre le bouton du clic here.It worked.but à ce moment-là, il n'a pas reconnu que la cellule est enregistrée. NOTE: J'ai essayé d'implémenter la méthode de clic de bouton ici. J'ai travaillé. Mais à ce point il n'a pas identifié quelle cellule est enregistrée. **

Répondre

2

Vous pouvez mettre en œuvre par deux voies que vous pouvez ajouter Bouton Action à votre cellForRowAtIndexPath et l'étiquette mise Button comme le code suivant:

hidebutton.tag=indexPath.row; 

[hidebutton addTarget:self 
       action:@selector(hideaction:) 
     forControlEvents:UIControlEventTouchUpInside]; 

Sa méthode d'action est

-(IBAction)hideaction:(UIButton*)sender 
{ 
    NSIndexPath *hideIndexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 
    TestTableViewCell *cell = (TestTableViewCell *)[self.tablev cellForRowAtIndexPath:hideIndexpath]; 

} 

Une autre façon est que vous pouvez obtenir la même chose à partir de la méthode DidSelect avec le code suivant:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
      TestTableViewCell *cell = (TestTableViewCell *)[self.tablev cellForRowAtIndexPath:indexPath]; 
//use your cell object for hide anyting 
    } 
+0

agréable et merci beaucoup.J'ai essayé différentes façons, n'a pas réussi. c'est bien. –

+0

vous pouvez le faire aussi par diff de façon @ bhavin's answe également droit, vous pouvez le faire par ce trop –

1

Vous pouvez obtenir indexPath.row de UITableView dans le bouton Action:


action Marque bouton yourviewcontroller.h fichier:

- (IBAction) My_button:(id)sender; 

Dans le fichier yourviewcontroller.m:

- (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); 
} 

Et si vous voulez le faire dans votre didSelectRowAtIndexPath, vous n'avez plus besoin de réinitialiser la cellule.

- (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; 
} 
0

vous avez besoin pour obtenir la première cellule correcte, vous pouvez y arriver en remplaçant

TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"]; 

par ceci:

TestTableViewCell *cell = (TestTableViewCell*)[tableView cellForRowAtIndexPath:indexPath]; 

Votre méthode:

- (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 Aide