【问题标题】:Data not Appearing on UIView数据未出现在 UIView 上
【发布时间】:2015-07-15 05:17:47
【问题描述】:

我有一个标题 UIView,我正在尝试将 JSON 数据加载到其中,但是根本没有显示任何信息,如图所示:

这是我加载标题视图的方法:

- (ForeignInstagramViewHeader *)header {
    if (!_header) {
        _header = [[ForeignInstagramViewHeader alloc] initWithFrame:CGRectMake(0, 0, 320, 150) withUserData:self.instaUser];
        NSString *userID = self.instagramData[@"user"][@"id"];
        [self getInstaDataWithUserID:userID];
        _header.instaUser = self.instaUser;
    }
    return _header;
}

这是我获取 Instagram 数据的方法:

- (void)getInstaDataWithUserID:(NSString *)user {
    NSString *path = [NSString stringWithFormat:@"users/%@", user];

     [[InstagramClient sharedClient] getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"%@",responseObject);
         self.instaUser = [[InstagramUser alloc] init];
         [self.instaUser setBio:responseObject[@"data"][@"bio"]];
         self.instaUser.followed_by = responseObject[@"data"][@"counts"][@"followed_by"];
         self.instaUser.follows = responseObject[@"data"][@"counts"][@"follows"];
         self.instaUser.media = responseObject[@"data"][@"counts"][@"media"];
         self.instaUser.full_name = responseObject[@"data"][@"full_name"];
         self.instaUser.idvalue = responseObject[@"data"][@"id"];
         self.instaUser.profile_picture = responseObject[@"data"][@"profile_picture"];
         self.instaUser.username = responseObject[@"data"][@"username"];
         self.instaUser.website = responseObject[@"data"][@"website"];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"Error: %@", error);
     }];
}

这是我的标题 View Class.h :

@interface ForeignInstagramViewHeader : UIView

@property (nonatomic, strong) UIImageView *profilePic;
@property (nonatomic, strong) UILabel * nameString;
@property (nonatomic, strong) UILabel * postCount;
@property (nonatomic, strong) UILabel * postString;
@property (nonatomic, strong) UILabel * followerCount;
@property (nonatomic, strong) UILabel * followerString;
@property (nonatomic, strong) UILabel * followingCount;
@property (nonatomic, strong) UILabel * followingString;
@property (nonatomic, strong) UILabel * bioString;
@property (nonatomic, strong) UILabel * websiteString;

@property InstagramUser *instaUser;

- (id)initWithFrame:(CGRect)frame withUserData:(InstagramUser *)instagram;
@end

【问题讨论】:

  • 用户数据getPath:完成后需要更新数据到header view。
  • @Akhilrajtr 我该怎么做??

标签: ios objective-c json uiview uiviewcontroller


【解决方案1】:

ForeignInstagramViewHeader 类中添加一个使用用户详细信息更新 UI 的方法,例如,

- (void)updateWithUserDetails:(InstagramUser *)user {

    self.instaUser = user;
    // set values from user to lable's and other UI elements.
}

头方法将是

- (ForeignInstagramViewHeader *)header {
    if (!_header) {
        _header = [[ForeignInstagramViewHeader alloc] initWithFrame:CGRectMake(0, 0, 320, 150) withUserData:self.instaUser];
    }
    return _header;
}

getInstaDataWithUserID: 方法中,

- (void)getInstaDataWithUserID:(NSString *)user {
    NSString *path = [NSString stringWithFormat:@"users/%@", user];

     [[InstagramClient sharedClient] getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"%@",responseObject);
         self.instaUser = [[InstagramUser alloc] init];
         [self.instaUser setBio:responseObject[@"data"][@"bio"]];
         self.instaUser.followed_by = responseObject[@"data"][@"counts"][@"followed_by"];
         self.instaUser.follows = responseObject[@"data"][@"counts"][@"follows"];
         self.instaUser.media = responseObject[@"data"][@"counts"][@"media"];
         self.instaUser.full_name = responseObject[@"data"][@"full_name"];
         self.instaUser.idvalue = responseObject[@"data"][@"id"];
         self.instaUser.profile_picture = responseObject[@"data"][@"profile_picture"];
         self.instaUser.username = responseObject[@"data"][@"username"];
         self.instaUser.website = responseObject[@"data"][@"website"];

         //UPDATE
         dispatch_async(dispatch_get_main_queue(), ^{
             [[self header] updateWithUserDetails:self.instaUser];
         });
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"Error: %@", error);
     }];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多