将数据从tableview控制器传递到详细信息表视图控制器

时间:2015-09-22 02:18:09

标签: ios objective-c uitableview

我有以下代码。我想显示所选的数据。但是,下面的代码显示数据库中的每个帖子。我希望它只显示所选的那个。我怎么能这样做?

发布表视图控制器:

- (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;
}

详情查看控制器:

- (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;
}

2 个答案:

答案 0 :(得分:0)

您应该在 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];

}

和执行segue

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

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

}

HTH

答案 1 :(得分:0)

在表视图控制器中(从中发送数据)创建详细信息表视图控制器的对象(您必须在其上显示数据),按以下方式,

- (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];
    
}

传递要在行选择

上传递的数据

以下是一些链接,显示如何将数据从一个视图控制器传递到另一个视图控制器,其中一些链接溢出本身。

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