UICollectionView datasource methods not getting ca

2019-04-20 08:18发布

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?

7条回答
一夜七次
2楼-- · 2019-04-20 09:03

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.

查看更多
够拽才男人
3楼-- · 2019-04-20 09:08

Add the collectionView to a view hierarchy.

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.

查看更多
forever°为你锁心
4楼-- · 2019-04-20 09:20

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:

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

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

查看更多
乱世女痞
5楼-- · 2019-04-20 09:21

A couple things:

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

_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.

@interface JustinViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>
查看更多
仙女界的扛把子
6楼-- · 2019-04-20 09:23

A few suggestions:

  • Do all your UICollectionView setup and configuration in viewDidLoad.
  • Ensure you calling the create init method from your other class
  • Your tweetArray is also empty, so if the number of items method is called, it will return nothing and the other methods will not be called
查看更多
疯言疯语
7楼-- · 2019-04-20 09:24

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

查看更多
登录 后发表回答