ios UICollectionView 数据源方法未被调用,但正在初始化中设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19204954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:07:03  来源:igfitidea点击:

UICollectionView datasource methods not getting called, but are being set in the init

iosobjective-cuicollectionview

提问by Justin Cabral

Here is my source code

这是我的源代码

- (id)initWithCollectionView:(UICollectionView *)collectionView
{
self = [super init];

if (self)
{
    self.collectionView = collectionView;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[TWNTweetCell class] forCellWithReuseIdentifier:kCell];
    self.collectionViewLayout = self.collectionView.collectionViewLayout;



    self.tweetArray = @[];
    self.tweetTextArray = @[];

    self.twitter = [STTwitterAPI twitterAPIOSWithFirstAccount];
}

return self;
}

#pragma mark - CollectionView
#pragma mark DataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:      (NSInteger)section
{
return [self.tweetArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
TWNTweetCell *cell = (TWNTweetCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCell forIndexPath:indexPath];

NSDictionary *status = [self.tweetArray objectAtIndex:indexPath.row];

NSString *text = [status valueForKey:@"text"];

cell.usernameLabel.text = screenName;
//    cell.createdAtLabel.text = dateString;

cell.autoresizingMask = UIViewAutoresizingFlexibleWidth;

UITextView *textView = [self.tweetTextArray objectAtIndex:indexPath.row];
[cell setTweet:text withTweetTextView:textView];

return cell;
}

All the methods don't get interupted at all by breakpoints. The tweets are getting loaded in the log so I know everything else is ok, its just not recognizing the collection view. And yes i've set the

所有方法都不会被断点中断。推文正在加载到日志中,所以我知道其他一切都正常,只是无法识别集合视图。是的,我已经设置了

Anyone have any idea whats going on?

任何人都知道发生了什么?

回答by Julian Król

It is not your case, it might be helpful for others who will came here having problem with data source methods not being called. It could be assigning data source like:

这不是你的情况,它可能对来这里遇到数据源方法未被调用的问题的其他人有所帮助。它可以分配数据源,如:

collectionView.dataSource = MyDataSource()

which is wrong as dataSource is a weak reference so it needs to be stored by some strong reference to be alive after creating it. Added a private property in a ViewController to keep the strong reference, initialising and then assigning it fixes the issue.

这是错误的,因为 dataSource 是弱引用,因此需要通过一些强引用存储它才能在创建后保持活动状态。在 ViewController 中添加了一个私有属性以保持强引用,初始化然后分配它修复了问题。

回答by Michael Dautermann

A couple things:

一些事情:

1) in (and only in) your "init" method, use the underlying instance variable for your @property. That is,

1)在(并且仅在)您的“ init”方法中,为您的@property 使用底层实例变量。那是,

_collectionView = collectionView;
_collectionView.dataSource = self;
_collectionView.delegate = self;

This is called "direct access", and more information can be seen in this related question.

这称为“直接访问”,更多信息可以在这个相关问题中看到

2) in your .h file, make certain to declare that your object conforms to the data source & delegate protocols. E.G.

2) 在您的 .h 文件中,确保声明您的对象符合数据源和委托协议。例如

@interface JustinViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>

回答by Tim

A few suggestions:

几点建议:

  • Do all your UICollectionViewsetup and configuration in viewDidLoad.
  • Ensure you calling the create initmethod from your other class
  • Your tweetArrayis also empty, so if the number of items method is called, it will return nothing and the other methods will not be called
  • 做你UICollectionView的设置和配置viewDidLoad
  • 确保您init从其他类调用 create方法
  • tweetArray的也是空的,所以如果调用了 number of items 方法,它不会返回任何内容,其他方法也不会被调用

回答by user2403271

for swift do this , set a property

为了迅速做到这一点,设置一个属性

//MARK: props
let dataSource = MyDataSource()

and in

并在

viewDidLoad(){
// your other code 
..
..
collectionView.dataSource = dataSource // it is a strong reference 
}

apart form these other general pitfall are

除了这些其他一般陷阱

  • not returning the count or the data source
  • not populating the data source
  • 不返回计数或数据源
  • 不填充数据源

回答by user3378170

Add the collectionView to a view hierarchy.

将 collectionView 添加到视图层次结构。

In the init method you set the property (self.collectionView) but you do not add the collectionView to a view hierarchy. So the collectionView won't call any dataSource or delegate method.

在 init 方法中,您设置了属性 (self.collectionView),但没有将 collectionView 添加到视图层次结构中。所以 collectionView 不会调用任何 dataSource 或委托方法。

回答by Ali Awais

I created collection view in storyboard and linked datasource and delegate but they were not being called in Xcode 8.0 with Swift 3.0. Tried multiple things but the solution was to declare the delegate and datasource in class declaration line:

我在情节提要中创建了集合视图并链接了数据源和委托,但它们没有在 Xcode 8.0 中使用 Swift 3.0 调用。尝试了多种方法,但解决方案是在类声明行中声明委托和数据源:

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { ... }

类 ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { ... }

Previously when we linked delegate and datasource through storyboard it was not required, may be a bug :)

以前当我们通过故事板链接委托和数据源时,它不是必需的,可能是一个错误:)

回答by Timothy Moose

Call [collectionView reloadData]at the end of your initmethod. The collection view needs to be told to populate itself. I assume UICollectionViewControllerdoes this internally, but you don't seem to be using UICollectionViewController(or at least not in the usual way).

[collectionView reloadData]init方法结束时调用。集合视图需要被告知填充自身。我假设UICollectionViewController在内部执行此操作,但您似乎没有使用UICollectionViewController(或至少没有以通常的方式使用)。