Sajin
Sajin

Reputation: 13

Selecting the text in the tableview cell and returning to previous view

I'm quite new to this site and iOS development. I have an app that I'm working on, in it, I'm working on a view where when the users taps one button named "zones" the view calls a table view with a list of zones. I'm currently able to make a selection. But what I want is to return back to the previous view when user makes a selection on that table and I want to copy that text in the selection to a variable. Tried so many things. Nothing came close :(

   - (void)viewDidLoad
  {
[super viewDidLoad];


 zones=[NSArray arrayWithObjects:@"392A", @"382F", @"388A", @"373A", @"372A", @"373B", @"366A", @"367A", @"368B", @"362A", @"364A", @"356A",@"357A", @"364B", @"354A",@"353A", @"342A", nil];
NSLog(@"Apple");
// Do any additional setup after loading the view.
}

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell           forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Home.png"]];
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
return [zones count];
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
static NSString *tableIdentifier = @"zonesTable";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];



if (cell==nil)
{
    cell=[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:tableIdentifier];

}

cell.textLabel.text = [zones objectAtIndex:indexPath.row];

return cell;

}

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

 {

UITableViewCell *cellValue = [tableView cellForRowAtIndexPath:indexPath];

NSLog(@"%@", cellValue); //Not Even getting the NSLog output in Xcode ;(


  } 

Upvotes: 1

Views: 515

Answers (1)

iBug
iBug

Reputation: 2352

Try this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.currentCityName = [[self.cityArray objectAtIndex:indexPath.row] cityName];
    [self.cityTableView reloadData];
    [self.delegate cityDidGetSelected:[self.cityArray objectAtIndex:indexPath.row]];

    //putting 1/3 of delay
    [self performSelector:@selector(cancelButtonPressed) withObject:nil afterDelay:0.3];
}

CancelButtonPressed Method:

-(void) cancelButtonPressed {
    [self dismissViewControllerAnimated:YES completion:nil];
}

I have been using a delegate method cityDidGetSelected. Implement it in your view controller and pass the zone or city name to it.

CancelButtonPressed will work if you are using presentViewController to show your tableView.

Upvotes: 1

Related Questions