Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 588
Justin Cabral Avatar asked Oct 06 '13 02:10

Justin Cabral


People also ask

What is the use of datasource and delegate in UICollectionView in IOS?

This method asks the data source object to provide a supplementary view to display in the collection view. It asks the datasource object whether the specified item can be moved to another location in the collectionview. This method moves the specified item to the specified indexpath.

How does swift implement UICollectionView?

Select the Main storyboard from the file browser. Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices.

What is collectionView?

A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content. Layouts.

How to add collection view controller?

Open the storyboard in a new editor tab. Select Collection View in storyboard document view, press option key and drag the pointer to ViewController class. You will see a new property added: @IBOutlet weak var collectionView: UICollectionView!


2 Answers

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.

like image 189
Julian Avatar answered Sep 29 '22 11:09

Julian


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
like image 44
Tim Avatar answered Sep 29 '22 10:09

Tim