UICollectionView数据源方法未被调用,但正在init中设置

时间:2013-10-06 02:58:35

标签: ios objective-c uicollectionview

这是我的源代码

- (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;
}

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

任何人都知道最新情况?

7 个答案:

答案 0 :(得分:41)

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

collectionView.dataSource = MyDataSource()

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

答案 1 :(得分:2)

一些事情:

1)在(并且仅在)“init”方法中,为@property使用基础实例变量。也就是说,

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

这称为“直接访问”,and more information can be seen in this related question

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

@interface JustinViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>

答案 2 :(得分:2)

一些建议:

  • UICollectionView
  • 中完成所有viewDidLoad设置和配置
  • 确保从其他类
  • 调用create init方法
  • 你的tweetArray也是空的,所以如果调用了items数的方法,它将不会返回任何内容,也不会调用其他方法

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

对于swift执行此操作,设置属性

//MARK: props
let dataSource = MyDataSource()

并在

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

除了这些其他一般陷阱之外

  • 不返回计数或数据源
  • 未填充数据源

答案 5 :(得分:0)

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

答案 6 :(得分:0)

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

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

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