简体   繁体   English

带按钮的自定义表格视图单元格

[英]Custom table view cell with button

I am using storyboard. 我正在使用情节提要。 I have a table view with 1 Prototype cell, and have four rows in table view. 我有1个样品电池的表图,并且具有表视图四行。 Each cell has a label,image view and a button. 每个单元都有一个标签,图像视图和一个按钮。 Now I want to show different data on next View Controller say(DetailsVc), each time when button is pressed. 现在,我想在每次按下按钮时在下一个视图控制器说(DetailsVc)上显示不同的数据。 Row1- Button click will open DetailsVC with some data. 单击“ Row1-按钮”将打开包含某些数据的DetailsVC。 Row2- Button click will open DetailsVC with different data. 单击Row2-按钮将打开包含不同数据的DetailsVC。 and same process for remaining rows. 其余行的处理相同。 Any suggestions. 有什么建议么。

@implementation DetailsVC

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
EmpEngmntTableViewCell *emp;

 if((emp.viewGalleryBtn.tag = 1))
{
    NSLog(@"tag num for 1st row is %ld",(long)emp.viewGalleryBtn.tag);
    self.title = @"Team Building Session";
}
}

The best is to use a delegate pattern. 最好是使用委托模式。 I added an answer here , you can use it for example. 在这里添加了一个答案,您可以使用它作为示例。

Here is an example enclosed: 这里是一个示例:

Custom Cell .h Class: 自定义单元.h类:

@protocol DetailVCProtocol <NSObject>
-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex;
@end

@property (nonatomic, retain) id<DetailVCProtocol> delegate;
@property (nonatomic, retain) IBOutlet UIButton *btn;

Then Synthesize it in .m file 然后将其合成为.m文件

Add this in m file : m文件中添加:

-(IBAction)loadDetails:(id)sender
{
    // ..... blah blah
    [self.delegate loadDetailScreenWithIndex:btn.tag];
}

The viewcontroller that loads this cell: 加载此单元格的viewcontroller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // create cell here

   cell.btn.tag = indexPath.row;
   cell.delegate = self;
}

-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex
{
  // Cretae DetailVC with parameter `rowIndex` to populate data
  [self presentViewController:detailVC animated:YES completion:nil];
}

In function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 在函数中- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

use this code 使用此代码

cell.btn.tag = indexPath.row;
[cell.btn addTarget:self action:@selector(btnTpd:) forControlEvents:UIControlEventTouchUpInside];

- (IBAction)btnTpd:(id)sender
{
UIButton *btn = (UIButton *)sender;
\\You can get which button is pressed by btn.tag
    \\Here call to next ViewController and pass data
}

In cellForRowAtIndexPath 在cellForRowAtIndexPath中

 cell.optionsBtn.tag=indexPath.row;
 [cell.optionsBtn addTarget:self action:@selector(showMyOptionBtn:) forControlEvents:UIControlEventTouchUpInside];

-(void)showMyOptionBtn:(UIButton *)sender
{
  EventDetailViewController *EventDetail=[[EventDetailViewController alloc]init];
  EventDetail.buttonTag=sender.tag;
  [self.navigationController pushViewController:EventDetail animated:NO];

 }

In EventDetailViewController class

   if (self.buttonTag==1) {
     // your condation
   }
   else
   {
    //your condation
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM