Reputation: 11201
I am trying to display the twitter feed in collection view. I am able to display the twitter feed in normal table layout but when i try to display it in collection view, nothing is displayed in my view controller. I dragged a view into my view controller and a collection view into my view.Here is the view of my storyboard:
viewcontroller->View-> View1 ->CollectionView -> CollectionViewCell-> Label,ImageView
Here I am having two views in my view controller. One is for my list view and other one is for collection view. I am not sure about what I missed here. Could you please help me in this.
Thanks in advance. Here are my files:
TweetCell.h
#import <UIKit/UIKit.h>
@interface TweetCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageTweet;
@property (weak, nonatomic) IBOutlet UILabel *textTweet;
@end
TweetCell.m
#import "TweetCell.h"
@implementation TweetCell
@synthesize textTweet,imageTweet;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CellID" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{
IBOutlet UICollectionView *collectionViewTwitter;
}
@end
ViewController.m
#import "ViewController.h"
#import "STTwitter.h"
#import "TweetCell.h"
@interface ViewController ()
@property (strong, nonatomic) NSMutableArray *twitterFeedList;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
/* uncomment this block to use subclassed cells */
[self->collectionViewTwitter registerClass:[TweetCell class] forCellWithReuseIdentifier:@"cvCell"];
/* end of subclass-based cells block */
// Configure layout
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(200, 200)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self->collectionViewTwitter setCollectionViewLayout:flowLayout];
STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"xz9ew8UZ6rz8TW3QBSDYg"
consumerSecret:@"rm8grg0aIPCUnTpgC5H1NMt4uWYUVXKPqH8brIqD4o"];
[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {
[twitter getUserTimelineWithScreenName:@"MYTwitterUserName"
successBlock:^(NSArray *statuses) {
self.twitterFeedList = [NSMutableArray arrayWithArray:statuses];
[self->collectionViewTwitter reloadData];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.twitterFeedList.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"CellID" ;
NSInteger idx = indexPath.row;
NSDictionary *t = self.twitterFeedList[idx];
/* Uncomment this block to use subclass-based cells */
TweetCell *cell = (TweetCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
cell.textTweet.adjustsFontSizeToFitWidth=YES;
cell.textTweet.numberOfLines=4;
cell.textTweet.text=t[@"text"];
cell.imageTweet.image=[UIImage imageNamed:@"twitter.png"];
/* end of subclass-based cells block */
// Return the cell
return cell;
}
Upvotes: 0
Views: 332
Reputation: 512
You need to make sure your collection view's datasource and delegate are hooked up to the file's owner. To do this, click on your collection view in the xib/storyboard file. Then in the connection inspector on the right, click and drag from the data source over to file's owner. Do the same for the delegate. That way the collection view knows who to ask for implementation of the collection view. alternatively, you can do self.collectionView.delegate = self
and self.collectionView.datasource = self
in viewDidLoad
or viewWillAppear
assuming you have your IBOutlet hooked up from the xib/storyboard file
Upvotes: 1