0

I am trying to add UICollectionView with custom UICollectionViewCell programmatically in my project.

Here is my implementation to add the CollectionView

- (void)setUpCollectionView{
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    [layout setMinimumLineSpacing:1];
    [layout setMinimumInteritemSpacing:1];
    [layout setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];

    self.collectionView =[[UICollectionView alloc] initWithFrame:self.vwChecklistsContainer.frame collectionViewLayout:layout];
    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];

    UINib *cellNib = [UINib nibWithNibName:@"MyChecklistsCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyChecklistsCell"];
    [self.vwChecklistsContainer addSubview:self.collectionView];
}

This "setUpCollectionView" function was called from ViewDidLoad() of my viewcontroller.

I just implemented these two function in MyChecklistsCell.

- (void)awakeFromNib {
     [super awakeFromNib];
    // Initialization code
}

- (void)setData:(NSDictionary *)dict{
    self.lblChecklistType = dict[SERVER_CHECKLIST_TYPE];
    self.lblEndTime = dict[SERVER_END_TIME];
    self.lblStartTime = dict[SERVER_START_TIME];
    self.lblDescription = dict[SERVER_DESCRIPTION];
}

Can anyone pls help me on this? Thanks.

Nyein Ei Ei Tun
  • 168
  • 2
  • 13
  • "App crashes": What's the error message? – Larme Oct 05 '17 at 09:59
  • @Larme only show this error msg "-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000043" – Nyein Ei Ei Tun Oct 05 '17 at 10:04
  • @NyeinEiEiTun problem in setData method – Dixit Akabari Oct 05 '17 at 10:07
  • 1
    Which line? It wouldn't surprise me that the issue appears on `setData:`. My guess? `value = dict[oneOfMoreOfThesesKey]` is a `NSNumber` and not a `NSString`. – Larme Oct 05 '17 at 10:07
  • It didn't even go to "setData". App crash after this line "[self.vwChecklistsContainer addSubview:self.collectionView];" – Nyein Ei Ei Tun Oct 05 '17 at 10:20
  • Finally I found the source of issue. You are correct. I need to set data to the labels from the view of myViewController before setting data to UICollectionView. When I set the data to my ViewController, I haven't convert the values to String. That's why I got the error. Thanks all for your answer. – Nyein Ei Ei Tun Oct 06 '17 at 02:58

2 Answers2

1

Try this.

- (void)setData:(NSDictionary *)dict{
        self.lblChecklistType = [NSString stringWithFormat:@"%@",dict[SERVER_CHECKLIST_TYPE]];
        self.lblEndTime = [NSString stringWithFormat:@"%@",dict[SERVER_END_TIME]]; ;
        self.lblStartTime = [NSString stringWithFormat:@"%@",dict[SERVER_START_TIME]]; ;
        self.lblDescription = [NSString stringWithFormat:@"%@",dict[SERVER_DESCRIPTION]];
  }

Error indicates you are using a NSString method on a NSNumber. Plese, check if your data are strings.

Hope it helps.

Dixit Akabari
  • 2,419
  • 13
  • 26
  • Strange! the delegate function "cellForItemAtIndexPath" was not even triggered. Because I called setData function inside that delegate. I got crash after this line "[self.vwChecklistsContainer addSubview:self.collectionView];" from "setUpCollectionView" function. – Nyein Ei Ei Tun Oct 05 '17 at 10:26
  • Finally I found the source of issue. You are correct. I need to set data to the labels from the view of myViewController before setting data to UICollectionView. When I set the data to my ViewController, I haven't convert the values to String. That's why I got the error. Thanks for your answer. – Nyein Ei Ei Tun Oct 06 '17 at 02:59
1

Looks like your trying to set a label to an NSNumber. The problem here is using a dictionary to hold your data without any type-checking. Consider using an object to store your data, rather than a dictionary. This would make it much clearer what properties your trying to set and avoid future issues.

Alternatively the error message you commented seems to only reference a memory address, rather than an object which could indicate your not properly retaining the collection view or other properties - have you double checked all references are properly strong / weak?

  • Strange! the delegate function "cellForItemAtIndexPath" was not even triggered. Because I called setData function inside that delegate. I got crash after this line "[self.vwChecklistsContainer addSubview:self.collectionView];" from "setUpCollectionView" function. – Nyein Ei Ei Tun Oct 05 '17 at 10:27
  • Finally I found the source of issue. You are correct. I need to set data to the labels from the view of myViewController before setting data to UICollectionView. When I set the data to my ViewController, I haven't convert the values to String. That's why I got the error. Thanks for your answer. – Nyein Ei Ei Tun Oct 06 '17 at 02:58