[英]Custom table view cell with button
我正在使用情節提要。 我有1個樣品電池的表圖,並且具有表視圖四行。 每個單元都有一個標簽,圖像視圖和一個按鈕。 現在,我想在每次按下按鈕時在下一個視圖控制器說(DetailsVc)上顯示不同的數據。 單擊“ Row1-按鈕”將打開包含某些數據的DetailsVC。 單擊Row2-按鈕將打開包含不同數據的DetailsVC。 其余行的處理相同。 有什么建議么。
@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";
}
}
最好是使用委托模式。 我在這里添加了一個答案,您可以使用它作為示例。
這里是一個示例:
自定義單元.h類:
@protocol DetailVCProtocol <NSObject>
-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex;
@end
@property (nonatomic, retain) id<DetailVCProtocol> delegate;
@property (nonatomic, retain) IBOutlet UIButton *btn;
然后將其合成為.m文件
在m文件中添加:
-(IBAction)loadDetails:(id)sender
{
// ..... blah blah
[self.delegate loadDetailScreenWithIndex:btn.tag];
}
加載此單元格的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];
}
在函數中- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
使用此代碼
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
}
在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.