【问题标题】:how to fire a button action method in didselectrowatindexpath in iOS, objective c如何在 iOS 的 didselectrowatindexpath 中触发按钮操作方法,目标 c
【发布时间】:2016-07-06 06:22:50
【问题描述】:

我有一个带有自定义表格视图单元格的表格视图。在 tableview 单元格中有两个标签和一个按钮。我希望它为用户选择的行触发按钮操作以隐藏同一行中的标签。

这是我的表格视图控制器

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 //我试图在这里实现按钮单击方法。它有效。但那时它没有识别出哪个单元格被录音。

**注意:我试图在这里实现按钮单击方法。我工作过。但那时它没有识别出哪个单元格被录音。 **

【问题讨论】:

    标签: ios objective-c button tableview didselectrowatindexpath


    【解决方案1】:

    您可以通过两种方式实现,一种可以将 Button Action 添加到您的 cellForRowAtIndexPath 并设置 Button 的标签,如以下代码:

    hidebutton.tag=indexPath.row;
    
    [hidebutton addTarget:self
                     action:@selector(hideaction:)
           forControlEvents:UIControlEventTouchUpInside];
    

    它的Action Method是

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

    【讨论】:

    • 很好,非常感谢。我尝试了不同的方法,但没有成功。这很棒。
    • 你也可以通过 diff 方式做到@bhavin 的回答,你也可以这样做
    【解决方案2】:

    您可以在按钮操作中获得UITableView 中的indexPath.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;
    }
    

    【讨论】:

      【解决方案3】:

      你需要先得到正确的单元格,你可以通过替换来实现

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

      希望对您有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-18
        • 2014-03-04
        相关资源
        最近更新 更多