Ios 单击用户名并转到配置文件
我使用Parse作为我的后端,有一个主视图,其中所有用户的照片都将显示,有点像流,每个单元格都有一个带有用户用户名的标题,您可以单击该标题并转到他们的个人资料。但是,我不知道如何访问用户信息并将其带到下一个视图。。这是到目前为止我的代码Ios 单击用户名并转到配置文件,ios,parse-platform,Ios,Parse Platform,我使用Parse作为我的后端,有一个主视图,其中所有用户的照片都将显示,有点像流,每个单元格都有一个带有用户用户名的标题,您可以单击该标题并转到他们的个人资料。但是,我不知道如何访问用户信息并将其带到下一个视图。。这是到目前为止我的代码 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { static NSString *headerIdentifier = @
-(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,我非常困惑 我认为一个问题是按钮没有将用户参数中的任何内容传递给
-viewProfile:user
方法
虽然我不喜欢用“幻数”来做这类事情,但您可能需要做的是使用按钮上的.tag
属性作为索引,然后通过用户照片
查找用户。所以
加载项-tableView:viewForHeaderInSection:
button.tag = section
改变
[button addTarget:self action:@selector(viewProfile: user:) forControlEvents:UIControlEventTouchUpInside];
到
并将-viewProfile:user:
方法替换为
- (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];
}
您可能需要也可能不需要再次获取该用户-我不确定在获取该用户时,Parse SDK是否会在userPhotos中更新该用户。如果您在查询中使用。includeKey(“用户”)
来获取用户照片,您也可以完全避免抓取。我的朋友,我想感谢您的帮助,它非常有效。@DerekSaunders如果它对您有效,您能接受答案吗?
- (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];
}