-1

I have a UITableView and I add cells with a button on the UIViewController.

If the cells are tapped, you are brought to a new viewController which has a picker.

How can I set the the selected cell's text as title for next viewController?

i_duhh_bomb
  • 300
  • 1
  • 10

5 Answers5

3
IF YOU WANT TO SEND SELECTED ROW's TITLE TO NEXT VIEW CONTROLLER (push)
  1. Create property (say title) in your new view controller's .h file to hold title of selected row and synthesize title property in your new view controller's .m file.

  2. update didSelectRowAtIndexPath

     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath 
    
    
     {
     YourNewViewController *vc= [[YourNewViewController          
     alloc]initWithNibName:@"YourNewViewControllerNIB" bundle:nil];
    
     vc.title=[[titleArray objectAtIndexPath]indexPath.row];
    
     [self.navigationController pushViewController:vc animated:YES];
    
     }
    
  3. now you have selected row's title in your new view controller.

  4. for setting this title as navigation bar title write this code to viewDidLoad of your new view controller.

    self.navigationItem.title=title.

IF YOU WANT TO SEND SELECTED ROW's TITLE TO PREVIOUS VIEW CONTROLLER (POP)

for this you have to use delegate pattern and you have to make custom protocols.

follow this. - Passing Data between View Controllers

Community
  • 1
  • 1
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
2

// This is how to get selected cell

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]];

cell.textLabel.text = @"Some title";
B.S.
  • 21,660
  • 14
  • 87
  • 109
2

In the didSelectRowAtIndexpath method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *Cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *NewTitle = @"Add your new title here";
IronManGill
  • 7,222
  • 2
  • 31
  • 52
2

Write this code in either button's method OR your picker's method

      UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]];
      cell.textLabel.text = self.MyButtonName.titleLabel.text;
      [self.tableView reloadData];
iPatel
  • 46,010
  • 16
  • 115
  • 137
1

Are you looking for the selected row?

- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
    [cell.textLabel setText:@"Your Title"];
    [_tableView reloadData];

}
Nabeel Thobani
  • 939
  • 10
  • 23