单击用户名并转到个人资料

时间:2014-10-29 23:11:14

标签: ios parse-platform

我正在使用Parse作为我的后端,并且有一个主视图,所有用户的照片都会显示,有点像流,每个单元格都有一个标题,其中包含用户的用户名,您可以点击它并转到他们的个人资料。但是,我无法弄清楚如何访问用户的信息并将其带到下一个视图。到目前为止,这是我的代码。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    static NSString *headerIdentifier = @"headerIdentifier";
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerIdentifier];

    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, self.view.bounds.size.width, 44.0f)];


        //Container to hold everything.
        self.containerView = [[UIView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, self.view.bounds.size.width, 44)];
        self.containerView.backgroundColor = [UIColor clearColor];
        [headerView.contentView addSubview:containerView];


        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button addTarget:self
                   action:@selector(viewProfile: user:)
         forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.frame = CGRectMake(8, 6, 100, 30);

        //Setting the username to button.
        PFObject *owner = [self.userPhotos objectAtIndex:section];
        PFUser *user = [owner objectForKey:@"user"];
        [user fetch];
        self.usernameHolder = user.username;
        [button setTitle:self.usernameHolder forState:UIControlStateNormal];

        [containerView addSubview:button];
    }

    return headerView;
}

- (void)viewProfile:(UIButton *)button user:(PFUser *)user {
    AccountView *accountView = [[AccountView alloc] init];
    [accountView setUser:user];
    [self.navigationController pushViewController:accountView animated:YES];
}

当我在accountView上执行NSLog时,它返回为null并且非常困惑!

1 个答案:

答案 0 :(得分:0)

我认为一个问题是按钮没有将用户参数中的任何内容传递给-viewProfile:user方法。

尽管我讨厌使用"魔术数字"对于这类事情,您可能需要做的是使用按钮上的.tag属性作为索引,然后通过user查找userPhotos。所以......

加入-tableView:viewForHeaderInSection:

button.tag = section

更改

[button addTarget:self action:@selector(viewProfile: user:) forControlEvents:UIControlEventTouchUpInside];

[button addTarget:self action:@selector(viewProfile:) forControlEvents:UIControlEventTouchUpInside];

将<{1}}方法替换为

-viewProfile:user:

您可能需要或可能不需要再次获取用户 - 我不确定Parse SDK是否会在您获取用户时更新userPhotos中的用户。如果您在查询中- (void)viewProfile:(UIButton *)button { AccountView *accountView = [[AccountView alloc] init]; PFObject *owner = [self.userPhotos objectAtIndex:button.tag]; PFUser *user = [owner objectForKey:@"user"]; [accountView setUser:user]; [self.navigationController pushViewController:accountView animated:YES]; } 获取userPhotos,您也可以完全避免获取。