Sudhir
Sudhir

Reputation: 61

Passing and accessing object data outside table view

I want to access object data that I used in my tableview method in my button action event, It is showing the error - "Use of undeclared identifier", Here is my code snippets-

TableView method-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"BlogTableViewCell";

    BlogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     long index1 = 2 * (indexPath.row);
    long index2 = index1 + 1;
    if (index1 < [self.BlogsArray count]) {
        cell.vw_blog1.tag = index1;
    BlogModel *blog = [self.BlogsArray objectAtIndex:index1];
        cell.lbl_blogTitle1.text = blog.blogTitle;
        cell.lbl_blogDescripton1.text = blog.blogDesc;
}

My button Action method -

- (IBAction)btnAction_AuthorName1:(id)sender {

      NSLog(@"Author1 of blog1 is clicked");
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Consult" bundle:nil];
      DRDetailsViewController *DRDetailVC = (DRDetailsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DRDetailsViewController"];
      DRDetailVC.DoctorID = [NSString stringWithFormat:@"%@",blog.userId];
      [APP_DELEGATE.navigationController pushViewController:DRDetailVC animated:YES];

}

It is showing error at blog.UserId .

Upvotes: 0

Views: 58

Answers (2)

Kuldeep
Kuldeep

Reputation: 4552

Add Tag and create Button action in cellForRowAtIndexPath method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"BlogTableViewCell";

    BlogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

You Button Action code.

- (IBAction)btnAction_AuthorName1:(UIButton *)sender {

    BlogModel *blog = [self.BlogsArray objectAtIndex:sender.tag];

    NSLog(@"Author1 of blog1 is clicked");
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Consult" bundle:nil];
    DRDetailsViewController *DRDetailVC = (DRDetailsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DRDetailsViewController"];
    DRDetailVC.DoctorID = [NSString stringWithFormat:@"%@",blog.userId];
    [APP_DELEGATE.navigationController pushViewController:DRDetailVC animated:YES];
}

Upvotes: 1

aBilal17
aBilal17

Reputation: 3132

Add Tag in cellForRowAtIndexPath method for your button.

cell.btn.tag = indexPath.row;

and then get the tag value for selected index

- (IBAction)btnAction_AuthorName1:(UIButton *)sender {

  BlogModel *blog = [self.BlogsArray objectAtIndex:sender.tag];

  NSLog(@"Author1 of blog1 is clicked");
  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Consult" bundle:nil];
  DRDetailsViewController *DRDetailVC = (DRDetailsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DRDetailsViewController"];
  DRDetailVC.DoctorID = [NSString stringWithFormat:@"%@",blog.userId];
  [APP_DELEGATE.navigationController pushViewController:DRDetailVC animated:YES];

}

Upvotes: 1

Related Questions