gpt4 book ai didi

ios - 在 Collection View 的 cellForItemAtIndexPath 方法中更改框架

转载 作者:行者123 更新时间:2023-11-29 12:34:06 25 4
gpt4 key购买 nike

我是 IOS 新手。

我用过 Collection View 。我在其中使用原型(prototype)单元格。我没有在我的应用中使用自动布局。

我需要在 cellForItemAtIndexPath 中更改按钮和 View 的框架。但是当我第一次滚动移动到下一个单元格时,它没有反射(reflect)第一次。我只能看到没有任何按钮或 View 的空白。我检查了所有的框架是否正确更新。我仍然看不到任何东西。

不过,如果我再次转到上一个电话并返回,它会正常显示。

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

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

感谢您的帮助。

最佳答案

添加此行应该可以解决问题。让单元格的 subview 将它们的框架转换为约束。

cell.label.translatesAutoresizingMaskIntoConstraints = YES;

关于ios - 在 Collection View 的 cellForItemAtIndexPath 方法中更改框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26818564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com