在集合视图中更改框架的cellForItemAtIndexPath方法

时间:2014-11-08 15:14:25

标签: ios iphone ipad uicollectionview uicollectionviewcell

我是IOS的新手。

我使用了集合视图。我正在使用Prototype单元格。我没有在我的应用程序中使用自动布局。

我需要在cellForItemAtIndexPath中更改按钮框架和视图。但是,当我第一次滚动到下一个单元格时,它没有反映第一次。我只能通过任何按钮或视图看到空白。我检查了所有帧都正确更新。我仍然看不到任何东西。

虽然如果我再次拨打上一个电话并回来,它将会正常显示。

请在下面找到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;

我还附上了相同的屏幕截图。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

添加此行应该可以解决问题。让单元格的子视图将其框架转换为约束。

cell.label.translatesAutoresizingMaskIntoConstraints = YES;