0

I am using storyboard. I have a table view with 1 Prototype cell, and have four rows in table view. 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. Row1- Button click will open DetailsVC with some data. Row2- Button click will open DetailsVC with different data. 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";
}
}
Jincy Sam
  • 421
  • 2
  • 11
simardeep singh
  • 220
  • 2
  • 11
  • Could not understand what you've done. can you please add more code for `didSelectRowAtIndexPath` and `DetailVC`? – ChintaN -Maddy- Ramani Sep 03 '15 at 06:21
  • Actually I haven't added any code in didSelectRowAtIndexPath. DetailsVC is the next view controller which will open on click of button in previous View controller in custom table view cell. I have made push relation of button with DetailsVC. Now what I want is, Row1- Button click will open DetailsVC with some data. Row2- Button click will open DetailsVC with different data. and same process for remaining rows – simardeep singh Sep 03 '15 at 06:29
  • selector of your button push button tag with DetailVC ,now in DetailVC check your button tag and make your condation. – Ram Vinay Yadav Sep 03 '15 at 06:30
  • @RamVinayYadav Can you provide some dummy example or some code? – simardeep singh Sep 03 '15 at 06:31
  • 1
    see this link http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Anbu.Karthik Sep 03 '15 at 06:32
  • 1
    then you need to pass data from 1st Viewcontroller to DetailVC. Without passing data you will not able to get data in DetailVC. Refer http://stackoverflow.com/questions/8429088/ios-storyboard-passing-data-navigationviewcontroller and http://stackoverflow.com/questions/11535915/passing-data-between-view-controllers-from-uitableview-to-a-details-view-contr – ChintaN -Maddy- Ramani Sep 03 '15 at 06:33
  • -(void)showMyOptionBtn:(UIButton *)sender { EventDetailViewController *EventDetail=[[EventDetailViewController alloc]init]; EventDetail.buttonTag=sender.tag; [self.navigationController pushViewController:EventDetail animated:NO]; } – Ram Vinay Yadav Sep 03 '15 at 06:40
  • 1
    now in EventDetailViewController class check buttonTag and make condation – Ram Vinay Yadav Sep 03 '15 at 06:41

3 Answers3

1

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:

@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

Add this in m file:

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

The viewcontroller that loads this cell:

- (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];
}
Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
-1

In function - (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
}
baydi
  • 1,003
  • 6
  • 11
-1

In 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
   }
Ram Vinay Yadav
  • 105
  • 2
  • 8