0

I have a table view, and if a certain row is tapped, the detail view will show up. But how does the detail view know which row was tapped ? So for instance, I have a MainController which displays names. If I tap "Johnny" The next screen should show a label with the string "Johnny". How would I do that ?

[EDIT - Added code]

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleView *article = [self.storyboard instantiateViewControllerWithIdentifier:@"ArticleView"];
    [article.myLabel setText:@"random"];
    [self.navigationController pushViewController:article animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *info = [json objectAtIndex:indexPath.row];
    cell.textLabel.text = [info objectForKey:@"username"];
    cell.textLabel.backgroundColor = nil;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.backgroundColor = nil;
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.text = [info objectForKey:@"user_pic"];

    // Configure the cell...

    return cell;
}

ArticleView.h

@interface ArticleView : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *myLabel;       
@end

ArticleView.m

-> Synthesizing properties

the_critic
  • 12,720
  • 19
  • 67
  • 115

3 Answers3

2

You could pass a object (i.e. the String Jonny) to the detail view controller as a property.

@interface ArticleViewController : UIViewController
@property (retain) ArticleView *articleView;
@end

//Don't forget to synthesize name 

in the tableview controller

-(void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath)indexPath
{
    NSDictionary *info = [json objectAtIndex:indexPath.row];
    ArticleViewController *articleViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ArticleViewController"]; 
    articleViewController.articleView = articleView;
    [self.navigationController pushViewController: articleViewController animated:YES];
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • You probably meant to type `dvc.name = [self.names objectAtIndex:indexPath.row];` – Kevin Jan 20 '12 at 15:08
  • :) You should have reload your page, I fixed that in-between. – vikingosegundo Jan 20 '12 at 15:08
  • I have a UILabel `myLabel` in my `DetailViewController`. This is how I did it (it does not work): `ArticleView *article = [self.storyboard instantiateViewControllerWithIdentifier:@"ArticleView"]; [article.myLabel setText:@"Johnny"];` – the_critic Jan 20 '12 at 15:15
  • You should be able, to adept my code, to pass in a label instead of the string. For more help, we will have to know more about your dataSource. Please post relevant code to your question. not in comments. – vikingosegundo Jan 20 '12 at 15:18
  • Information we need for further guidance: What is ArticleView — a cell or just a view? if just a view, how is it added to the cell? please post your `tableView:cellForRowAtIndexPath:` in your question. – vikingosegundo Jan 20 '12 at 15:30
  • I ask for `tableView:cellForRowAtIndexPath:` – vikingosegundo Jan 20 '12 at 15:37
  • no i don't. there are as many cells as there are objects in my json object. the rows are assigned the usernames of the json object.Evreything works fine in my table view. Names are shown as desired. Only the detailview won't work. – the_critic Jan 20 '12 at 16:00
  • Ahh, just realize, that your ArticleVuew is a controller. You should not do something like that. Call a view a view And a controller controller! – vikingosegundo Jan 20 '12 at 16:13
  • Well, actually there is no `DetailViewController`. I just used it to be unspecific about the naming. So my `ArticleView` (I know I did a mistake with its naming) is actually the `DetailViewController`... – the_critic Jan 20 '12 at 16:16
  • sure, that's why i removed DetailViewController from my code. But because of your inappropriate naming I had introduce it in the first place. – vikingosegundo Jan 20 '12 at 16:21
0

the table view delegate has the method tableView:didSelectRowAtIndexPath:, in this method you present your detail view so you know which row was selected from the indexPath parameter and can pass any info you need when you create the new view controller

wattson12
  • 11,176
  • 2
  • 32
  • 34
0

Assuming you're talking iPhone here, I would have a property name on my detail view:

@propery (nonatomic, retain) NSString * name;

and an init method as follows:

- (id) initWithName: (NSString *) name
{
   self = [super initWithNibName: @"<view-controller-nib-name>" bundle: nil];
   if (self) {
      self.name = name;
   }
   return self;
}

and then set name label in viewDidLoad

In the tableView:didSelectRowAtIndexPath: method, get the name referenced by the index path, create and init a detail view controller using initWithName:, then push on the nav stack.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • — here you find a discussion, why NSString-properties should be copied instead of retained. http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain – vikingosegundo Jan 20 '12 at 15:27