我正在使用SEMasonryView
在我的 iOS 应用程序中获取 pinterest 样式视图,但我似乎无法将 JSON 连接到每个单独的框。每次我运行它只是返回的代码
“2013-06-06 15:48:35.160 MasonryViewDemo[7366:c07] 宽度:320.000000”在控制台中。
这是我在 MainViewController 中所做的事情:
- (void)fetchData {
// set your MasonryView to be in loading state
self.masonryView.loading = YES;
// disable the reload button
reloadButton.enabled = NO;
// load data from Flickr's API. In this case, a search with the keyword "Lego" is made and 10 results are returned in each page.
// The call uses AFNetworking Library, can be grabbed for free from https://github.com/AFNetworking/AFNetworking
// Of course, you can use any networking library or Apple's default libraries for this task
NSString *urlString = [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=from:marcofolio"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
SEJSONRequestOperation *operation = [SEJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
for (NSDictionary *photo in [[JSON dictionaryForKey:@"photos"] arrayForKey:@"text"]) {
// add each of the photo object to your array, so that you can reference them on other methods
[self.photos addObject:photo];
// create a cell for each photo and add it to your MasonryView
SEMasonryCell *cell;
if (self.masonryView.horizontalModeEnabled) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"HorizontalCell" owner:self options:nil] objectAtIndex: 0];
cell.horizontalModeEnabled = YES;
// randomize image widths (for demonstration purposes only)
cell.imageWidth = 150 + arc4random() % 150;
} else {
cell = [[[NSBundle mainBundle] loadNibNamed:@"VerticalCell" owner:self options:nil] objectAtIndex: 0];
cell.horizontalModeEnabled = NO;
// randomize image heights (for demonstration purposes only)
cell.imageHeight = 50 + arc4random() % 150;
}
// set a tag for your cell, so that you can refer to it when there are interactions like tapping etc..
cell.tag = [self.photos indexOfObject:photo];
// set the cell's size, if you want to play with cell spacings and stuff, this is the place
if (screenWidth % 256 == 0) { // for iPad
if (self.masonryView.horizontalModeEnabled)
[cell setFrame:CGRectMake(10, 10, cell.frame.size.width + 15, 216)];
else
[cell setFrame:CGRectMake(10, 10, 236, cell.frame.size.height + 15)];
} else { // for iPhone
[cell setFrame:CGRectMake(10, 10, 140, cell.frame.size.height + 15)];
}
// finally, update the cell's controls with the data coming from the API
[cell updateCellInfo:photo];
// add it to your MasonryView
[masonryView addCell:cell];
}
// you are done loading, so turn off MasonryView's loading state
self.masonryView.loading = NO;
reloadButton.enabled = YES;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"error");
// if there has been an error, again turn of MasonryView's loading state
self.masonryView.loading = NO;
reloadButton.enabled = YES;
}];
[operation start];
// increase the counter so that the next time this method loads, it is gonna load the next page
pageCounter++;
}
在自定义单元格中:
- (void)updateCellInfo:(NSDictionary *)data {
self.text = [NSString stringWithFormat:@"%@ %@ %@", [data stringForKey:@"text"], [data stringForKey:@"text"], [data stringForKey:@"text"]];
[super updateCellInfo:data];
}