gpt4 book ai didi

ios - 无法在我的 View Controller 中看到 Collection View

转载 作者:行者123 更新时间:2023-11-29 02:40:58 26 4
gpt4 key购买 nike

我正在尝试在 Collection View 中显示 Twitter 提要。我能够以正常的表格布局显示 Twitter 提要,但是当我尝试在 Collection View 中显示它时,我的 View Controller 中没有显示任何内容。我将一个 View 拖到我的 View Controller 中,并将一个 Collection View 拖到我的 View 中。这是我的 Storyboard的 View :

 viewcontroller->View-> View1 ->CollectionView -> CollectionViewCell-> Label,ImageView

我的 View Controller 中有两个 View 。一个用于我的 ListView ,另一个用于 Collection View 。我不确定我在这里错过了什么。你能帮我做这件事吗?

提前致谢。这是我的文件:

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;

}

最佳答案

您需要确保 Collection View 的数据源和委托(delegate)已连接到文件的所有者。为此,请单击 xib/storyboard 文件中的 Collection View 。然后在右侧的连接检查器中,单击并从数据源拖动到文件所有者。对代表做同样的事情。这样 Collection View 就知道向谁请求 Collection View 的实现。或者,你可以做 self.collectionView.delegate = selfself.collectionView.datasource = selfviewDidLoadviewWillAppear假设你已经从 xib/storyboard 文件中连接了你的 IBOutlet

关于ios - 无法在我的 View Controller 中看到 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25811329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com