我正在尝试在集合视图中显示twitter Feed。我可以在正常的表格布局中显示twitter feed,但是当我尝试在集合视图中显示它时,我的视图控制器中没有显示任何内容。我将一个视图拖到我的视图控制器中,并将一个集合视图拖到我的视图中。这是我的故事板的视图:
viewcontroller->View-> View1 ->CollectionView -> CollectionViewCell-> Label,ImageView
这里我在视图控制器中有两个视图。一个是我的列表视图,另一个是集合视图。 我不确定我错过了什么。你能帮帮我吧吗?
提前致谢。 这是我的文件:
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;
}
答案 0 :(得分:1)
您需要确保您的收藏视图的数据源和代理已连接到文件的所有者。为此,请单击xib / storyboard文件中的集合视图。然后在右侧的连接检查器中,单击并从数据源拖动到文件的所有者。为代表做同样的事情。这样,集合视图就知道要求实现集合视图的人。或者,您可以在self.collectionView.delegate = self
或self.collectionView.datasource = self
中执行viewDidLoad
和viewWillAppear
,假设您已将您的IBOutlet与xib / storyboard文件挂钩