将CollectionView添加到我的ViewController

时间:2014-11-24 21:35:55

标签: ios objective-c xcode uicollectionview xcode6

下午好,

我第一次使用收藏视图,我已经按照以下网站的教程进行操作:http://www.techotopia.com/index.php/An_iOS_7_Storyboard-based_Collection_View_Tutorial#Adding_a_Collection_View_Controller_to_the_Storyboard

我有CollectionView正在运行,但现在我需要在ViewController中显示集合视图(因为在屏幕顶部我显示用户的配置文件,图像等。

如何在ViewController中显示CollectionView? 我必须将示例调整到我的ViewController中,但我对如何实现它有点遗失在我的真实应用程序中。

那是 ProfileViewController.h

#import <UIKit/UIKit.h>

@interface ProfileViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *profile;

@property (strong, nonatomic) IBOutlet UITextField *usersPassword;

@property (nonatomic, strong) NSMutableArray *jsonArray;

@property (strong, nonatomic) IBOutlet UILabel *followers;
@property (strong, nonatomic) IBOutlet UILabel *stars;
@property (strong, nonatomic) IBOutlet UILabel *pictures;
@property (strong, nonatomic) IBOutlet UIImageView *profileimage;
@property (strong, nonatomic) IBOutlet UILabel *username;

@end

那是我的 ProfileViewController.m

#import "ProfileViewController.h"
#import <Security/Security.h>
#import "SSKeychainQuery.h"
#import "SSKeychain.h"
#import "SBJson.h"

@interface NSURLRequest (DummyInterface)

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;

@end

@interface ProfileViewController ()

@end

@implementation ProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view setBackgroundColor: [self colorWithHexString:@"FFFFFF"]];

    self.profileimage.layer.cornerRadius = self.profileimage.frame.size.width / 2;
    self.profileimage.clipsToBounds = YES;
    self.profileimage.layer.borderWidth = 1.0f;
    self.profileimage.layer.borderColor = [UIColor whiteColor].CGColor;

    //NSString *usersPassword = [SSKeychain passwordForService:@"login" account:@"account"];
    //NSLog(@"Usuario ==> %@", usersPassword);

    [self fetchJson];
}

-(void)fetchJson {

    NSString *usersPassword = [SSKeychain passwordForService:@"login" account:@"account"];

    NSLog(@"usersPassword ==> %@", usersPassword);

    NSString *post =[[NSString alloc] initWithFormat:@"usersPassword=%@",usersPassword];
    //NSLog(@"PostData: %@",post);

    NSURL *url=[NSURL URLWithString:@"http://website.com/profile.php"];

    //NSData * data = [NSData dataWithContentsOfURL:url];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    //NSLog(@"Response code: %ld", (long)[response statusCode]);
    if ([response statusCode] >=200 && [response statusCode] <300)
    {
        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

        SBJsonParser *jsonParser = [SBJsonParser new];
        NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];

        NSInteger stars = [(NSNumber *) [jsonData objectForKey:@"stars"] integerValue];
        self.stars.text = [NSString stringWithFormat:@"%li", (long)stars];

        NSInteger followers = [(NSNumber *) [jsonData objectForKey:@"followers"] integerValue];
        self.followers.text = [NSString stringWithFormat:@"%li", (long)followers];

        NSInteger pictures = [(NSNumber *) [jsonData objectForKey:@"photos"] integerValue];
        self.pictures.text = [NSString stringWithFormat:@"%li", (long)pictures];

        self.username.text = [NSString stringWithFormat:@"*%@", usersPassword];

        NSString *picture = [jsonData objectForKey:@"picture"];
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:picture]];
        self.profileimage.image = [UIImage imageWithData:imageData];

        //NSLog(@"Imagen ==> %@", picture);

        NSLog(@"Perfil ==> %@", responseData);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIColor*)colorWithHexString:(NSString*)hex
{
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor grayColor];

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

    if ([cString length] != 6) return  [UIColor grayColor];

    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];

    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

@end

提前致谢。

0 个答案:

没有答案