我有一个带有自定义tableview单元格的tableview。在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 //我尝试在此处实现按钮单击方法。它起作用了,但是到那时它还没有识别出要粘贴哪个单元格。
**注意:我试图在此处实现按钮单击方法。我工作了。但是到那时,它无法识别要粘贴的是哪个单元格。**
您可以通过两种方式来实现,一种可以将Button Action添加到您cellForRowAtIndexPath
的Button中并设置Button的标签,如以下代码所示:
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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句