2016-07-06 66 views
0

我有一個帶有自定義tableview單元格的tableview。在tableview單元格中有兩個標籤和一個按鈕。我希望它激發用戶選擇的行的按鈕動作以隱藏同一行中的標籤。如何在iOS中的didselectrowatindexpath中觸發按鈕操作方法,目標c

這是我的表視圖控制器

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 //我試圖在這一點上它沒有認識到哪個小區錄音here.It worked.but實現按鈕點擊方法。

**注:我試圖在這裏實現按鈕單擊方法。我工作,但在那時它不能識別哪個單元格被錄製。 **

回答

2

您可以用兩種方法之一,你可以在你的cellForRowAtIndexPath添加按鈕操作執行和設置按鈕的標籤像下面的代碼:

hidebutton.tag=indexPath.row; 

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

及其作用方式是

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

} 

另一種方法是您可以使用以下代碼通過DidSelect方法實現此功能:

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

很好,非常感謝。我嘗試過不同的方式,沒有成功。這很棒。 –

+0

你也可以通過diff way @ bhavin的answe來做到這一點,你也可以做到這一點 –

1

您可以在按鈕動作得到UITableViewindexPath.row


按鈕的製作行動yourviewcontroller.h文件:

- (IBAction) My_button:(id)sender; 

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

而如果你想在你的didSelectRowAtIndexPath中這樣做,那麼你不需要再次出隊。

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

您需要首先得到正確的單元格,您可以通過此更換

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

實現它:

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

你的方法:

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

希望這幫助!

相關問題