简体   繁体   中英

Changing Frame in Collection View's cellForItemAtIndexPath Method

I am new to IOS.

I have used collection view. I am using Prototype cell in it. I have not used Auto-Layout in my app.

I need to change frame of buttons and view in cellForItemAtIndexPath . But when I first scroll to move to next cell it not reflecting first time. I can only see blank with out any buttons or view. I have checked all the frames are correctly updating. Still I can not see any thing.

Though if I again go to previous call and come back it will seen properly.

Please find below code of cellForItemAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{    
     Feed *feed;
    NSMutableAttributedString * string;

    feed = [arrTop objectAtIndex:indexPath.row];

    ShuffleCell *myCell = [collectionView
                         dequeueReusableCellWithReuseIdentifier:@"ShuffleCell"
                         forIndexPath:indexPath];

    myCell.index = indexPath;


    myCell.delegate = self;


   // Below frame changes are not working


        myCell.vwBack.frame = collect.frame;
        myCell.imgShare.frame = CGRectMake(myCell.imgShare.frame.origin.x, myCell.imgShare.frame.origin.y, self.view.frame.size.width,self.view.frame.size.width );
        myCell.lblText.center = CGPointMake(myCell.imgShare.frame.size.width / 2, myCell.imgShare.frame.size.height / 2);
        myCell.lblText.frame = CGRectMake(20
                                          , myCell.imgShare.frame.origin.y + 20, self.view.frame.size.width - 20,self.view.frame.size.width -20);

        int totHeight = (collect.frame.size.height - self.view.frame.size.width);
        myCell.vwBtn.frame = CGRectMake(myCell.vwBtn.frame.origin.x, collect.frame.size.height -totHeight, myCell.vwBtn.frame.size.width, totHeight);
        [myCell.vwBtn setFrame:CGRectMake(myCell.vwBtn.frame.origin.x, collect.frame.size.height -totHeight, myCell.vwBtn.frame.size.width, totHeight)];
        myCell.btnFavoriteBack.frame = CGRectMake(myCell.btnFavoriteBack.frame.origin.x, 0, myCell.btnFavoriteBack.frame.size.width, totHeight/3);
        [myCell.btnFavoriteBack setFrame:CGRectMake(myCell.btnFavoriteBack.frame.origin.x, 0, myCell.btnFavoriteBack.frame.size.width, totHeight/3)];
        myCell.btnShare.frame = CGRectMake(myCell.btnShare.frame.origin.x, (totHeight/3), myCell.btnShare.frame.size.width, totHeight/3);
        myCell.btnReport.frame = CGRectMake(myCell.btnReport.frame.origin.x, (totHeight/3)*2, myCell.btnReport.frame.size.width, totHeight/3);
        myCell.vwLikeBtn.frame = CGRectMake(self.view.frame.size.width - 82, (totHeight/3 - 32)/2, 82,32);

        NSLog(@"Y :- %f",myCell.vwBtn.frame.size.height);





    return myCell;

I have also attached screen shot of same.

Thanks for Help.

Adding this line should fix the problem. Let the subviews of cell translate their frames to constraints.

cell.label.translatesAutoresizingMaskIntoConstraints = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM