0

I have the following code. I want to display the data that is selected. However, below code is display every post in database. I would like it to display just the one that is selected. How can i do that?

Post Table View Controller:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

if (![self objectAtIndexPath:indexPath]) {
    // Load More Cell
    [self loadNextPage];
}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}

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

UserFeedTableViewCell *cell = (UserFeedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UserFeedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if (object) {
    cell.postView.text = [object objectForKey:@"post"];

    // PFQTVC will take care of asynchronously downloading files, but will only load them when the tableview is not moving. If the data is there, let's load it right away.

}

return cell;
}

Details View Controller:

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

CommentsTableViewCell *cell = (CommentsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[CommentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if (object) {
    cell.detailsPost.text = [object objectForKey:@"post"];


}

return cell;
}
  • You need to get the data just for the specified `indexPath`. – rmaddy Sep 22 '15 at 02:20
  • You haven't provided enough info. You should have some sort of array with one object per row in your table. How did you implement the `numberOfRowsInSection` method? That is typically based on the count of an array. Then in `cellForRowAtIndexPath`, you access one element from the array based on the `indexPath`. – rmaddy Sep 22 '15 at 02:23
  • Which of the two tables is causing your issue? What is `object` in the two table views? Show the code where you actually pass data to the details view controller. – rmaddy Sep 22 '15 at 02:55

2 Answers2

0

You should select a row in tableView:didSelectRowAtIndexPath

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

{

// store relevant value in your text object

[self performSegueWithIdentifier:@"showSegue" sender:self];

[self.tableView deselectRowAtIndexPath:indexPath animated:NO];

}

and the perform segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

 DetailViewController  *controller= [segue destinationViewController];
// set your text object here
 controller.text  = // your text object

}

HTH

Misha
  • 685
  • 8
  • 20
0

Create an object of the detail table view controller (on which you have to display the data) in the table view controller (from which data is sent), in the following way,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailTableViewController *objNext = (DetailTableViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"detailVC"];
    
    objNext.strLink=[[feeds objectAtIndex:indexPath.row] valueForKey:@"link"];
    
    [self.navigationController pushViewController:objNext animated:YES];
    
}

Pass the data which you want to pass on row select,

Here are some links which show how to pass data from one view controller to another, some of them are from stack overflow itself.

Passing Data between View Controllers

ios Pass data between navigation controller

https://teamtreehouse.com/community/how-to-pass-data-between-two-view-controllers-when-you-have-tab-bar-and-navigation-controller-in-between

Community
  • 1
  • 1
Arpit Dhamane
  • 503
  • 7
  • 19