Changing Frame in Collection View's cellForItemAtIndexPath Method - ios

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;

Related

Can't tap UICollectionView item

I'm building an app whereas I've got a UICollectionView with a custom layout. I have a problem whereas I cannot tap some rows. Here's an overview over what happens
1) App starts and presents a UICollectionView
2) 3 test items are displayed, using the visual debugger in Xcode, you can see that there is something not right with the hierarchy
The red row can't be tapped now, but the yellow and green rows can.
3) User taps the yellow item, and segues to another page
4) User pops the shown UIViewController and returns to the UICollectionView whereas the viewWillAppear method reloads the UICollectionView like so:
[self.collectionView reloadData];
5) Re-entering the visual debugger shows that the hierarchy seems shifted, and the yellow row is now untappable, but the red and green rows can be tapped.
What could be the reason for this? I'll post any relevant code, ask for what parts you'd like to see.
Update
The UIViewController displaying the UICollectionView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ShareViewCell *view = [collectionView dequeueReusableCellWithReuseIdentifier:#"share" forIndexPath:indexPath];
view.share = [self.sharesController shareAtIndex:indexPath.item];
return view;
}
Custom cell:
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.leftBorderView = [UIView new];
[self.contentView addSubview:self.leftBorderView];
self.label = [UILabel new];
self.label.font = [UIFont boldSystemFontOfSize:10];
self.label.numberOfLines = 0;
self.label.lineBreakMode = NSLineBreakByWordWrapping;
[self.contentView addSubview:self.label];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.leftBorderView.frame = CGRectMake(0, 0, 1.5, self.bounds.size.height);
CGSize labelSize = [self.label sizeThatFits:CGSizeMake(self.bounds.size.width - 10.0f, MAXFLOAT)];
NSTimeInterval durationOfShare = [self.share.endSpan timeIntervalSinceDate:self.share.startSpan] / 3600;
CGFloat middleOfCell = self.bounds.size.height / 2;
CGFloat yPositionToUse = middleOfCell - (labelSize.height / 2);
if (durationOfShare < 0.25)
{
[self.label setHidden:YES];// to small to be shown
} else if (durationOfShare > 0.5)
{
yPositionToUse = 8; // show it at the top
}
self.label.frame = CGRectMake(8, yPositionToUse, labelSize.width, labelSize.height);
}
Update 2
Is the UICollectionReusableView blocking tap of the UICollectionViewCell?
Ok,
First, you should be more precise in your question : you're using some code found on some question on StackOverflow.
The code seems to be this Calendar UI made with UICollectionView.
This is a sample code, quickly built for the sake of a StackOverflow answer (with a bounty). It's not finished, has no contributors, and you should try to read it and improve over it !
From your debugger's captures, it seems you have some view which overlays on top of your collectionView's cells.
From reading this code, I see it uses some supplementary view to present 'hour' blocks. This supplementary view class is HourReusableView. And it's the view coming on top on your debugger's captures
CalendarViewLayout is responsible to compute these supplementary views frame, as it does for colored event blocks (see method - (void)prepareLayout)
I might bet that these supplementary views's Z-order isn't predictable - all views have a default zIndex of 0. One way to fix it ? After line 67 of this file, set a negative zIndex on the UICollectionViewLayoutAttributes of the hour block. This way, you make sure hour supplementary views are always behind your cells, and don't intercept the cell's touch

UITableView rotation behaving strange after Xcode 6 and iOS 8 update

I am working on an app since a while, it is going well. However, this weekend I updated to Xcode 6 and now my app is behaving differently as opposed to before the update to Xcode 6.
I have a UITableView in my app which I rotate in viewDidLoad:
//Rotate playerCardsTable.
CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
_playerCardsTable.transform = rotateTable;
_playerCardsTable.frame = CGRectMake(0, 0, _playerCardsTable.frame.size.width, _playerCardsTable.frame.size.height);
In Xcode before the update (Xcode 5) I had this view:
But now, after updating to Xcode 6, I have this view:
The tableview is rotated, ergo I have horizontal scrolling, but it seems like the frame is not changed correctly after rotation. It is 320 pixels high and 80 pixels wide and it should be the other way round. I don't know why, but it seems I cannot change the frame afterwards in code, in other words, I don't see any change after changing the width and height.
The tableview is added via the interface builder and holds custom cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *cardName = [[[[[Game game] localPlayer] playerCards] objectAtIndex:indexPath.row] fileNameCard];
cell.cardImage.image = [UIImage imageNamed:cardName];
Card *card;
card = [[[[Game game] localPlayer] playerCards] objectAtIndex:indexPath.row];
if(card.playable == IsPlayable){
cell.backgroundColor = isPlayableColor;}
else if (card.playable == IsNotPlayable) {
cell.backgroundColor = isNotPlayableColor;}
else if (card.playable == IsReallyPlayable) {
cell.backgroundColor = isReallyPlayableColor;}
//Rotate image to align with rotated tableview.
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI/2);
cell.cardImage.transform = rotateImage;
cell.playableImage.transform = rotateImage;
cell.cardImage.layer.borderWidth = 2;
cell.cardImage.layer.borderColor = [UIColor clearColor].CGColor;
cell.cardImage.layer.shouldRasterize = YES;
cell.cardImage.layer.rasterizationScale = [[UIScreen mainScreen] scale];
cell.cardImage.layer.shadowColor = [UIColor blackColor].CGColor;
cell.cardImage.layer.shadowOffset = CGSizeMake(0, 1);
cell.cardImage.layer.shadowOpacity = 0.7;
cell.cardImage.layer.shadowRadius = 2.0;
cell.cardImage.clipsToBounds = NO;
return cell;}
To be clear; I did not change any code after the update, so the different behavior is caused by the update.
Hope you guys could help.
Thanks!
I ran into a very similar situation after upgrading iPad to ios8 just recently - Horizontal tables not appearing correctly.
I don't have a complete solution, but can tell you from research and testing that in my case it was due to the transform action and AutoLayout constraints on the UITableView not coordinating/cooperating. Evidently the way these two concepts work together changed somewhat in ios8.
A top constraint set in a storyboard becomes a left constraint after the transform (or right, never did figure it out properly).
Bottom line for me was that after trying a number of combinations of constraints, including removing them before transform, then adding constraints back in after the transform, I ended up converting from a UITableView to UICollectionView.

UICollectionView reloadData But View Blinks

I have an UICollectionView and a custom UICollectionViewCell to display my content, which should be refreshed every second.
I am going to invoke reloadData method to reload the whole collection view to fit my needs.
But, but, My collection view cell blinks every time I reload data.
It seems like the image below. Two seconds of my app. The first second is OK. But second second, the collection view display reused cell first (yellow area) and then display the correct cell(configured cell) finally. Which looks like a blink.
It seems like a cell-reuse issue. CollectionView displays cells without completely finish configure it. How could I fix it?
My cellForItemAtIndexPath: method:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
YKDownloadAnimeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[YKDownloadAnimeCollectionCell description] forIndexPath:indexPath];
YKAnimeDownloadTask *animeDownloadTask = [[YKVideoDownloadTaskManager shared] animeDownloadTasks][indexPath.row];
[cell setUpWithDownloadAnime:animeDownloadTask editing:self.vc.isEditing];
cell.delegate = self;
if (self.vc.isEditing) {
CABasicAnimation *imageViewAnimation = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
imageViewAnimation.fromValue = #(-M_PI/64);
imageViewAnimation.toValue = #(M_PI/128);
imageViewAnimation.duration = 0.1;
imageViewAnimation.repeatCount = NSUIntegerMax;
imageViewAnimation.autoreverses = YES;
[cell.coverImageView.layer addAnimation:imageViewAnimation forKey:#"SpringboardShake"];
CABasicAnimation *deleteBtnAnimation = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
deleteBtnAnimation.fromValue = #(-M_PI/32);
deleteBtnAnimation.toValue = #(M_PI/32);
deleteBtnAnimation.duration = 0.1;
deleteBtnAnimation.repeatCount = NSUIntegerMax;
deleteBtnAnimation.autoreverses = YES;
[cell.deleteBtn.layer addAnimation:deleteBtnAnimation forKey:#"SpringboardShake1"];
} else {
[cell.coverImageView.layer removeAllAnimations];
[cell.deleteBtn.layer removeAllAnimations];
}
return cell;
}
Well I found the answer though it's been so long from the question.
Don't use reloadData() nor reloadItems().
Use collectionView.reloadSections(IndexSet(integer: yourSectionIndex)). The animation will happen smoothly.
self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
Helped for me.

GMGridViewCell not shaking

I have used GMGridView inside my app. When I change the orientation all is fine, but if I change it again and enter edit mode all the cells are shacking except the visible one in the lower-right corner.
The GmGridView is added as a subview to a controller and it also doesn't occupy the whole screen. I've tried destroying it and recreating it when a rotation notification occurs ... same behaviour. Also I have made a custom view with multiple buttons and labels that I have set as the the GMGridViewCell's contentView.
here's the code for the cellForItemAtIndex
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
GMGridViewCell *cell = [gmGridView dequeueReusableCell];
if (!cell) {
CGSize cellSize = [self GMGridView:gmGridView sizeForItemsInInterfaceOrientation:orientation];
cell = [[GMGridViewCell alloc] initWithFrame:CGRectMake(0, 0, cellSize.width, cellSize.height)];
cell.deleteButtonIcon = [UIImage imageNamed:#"delete_button"];
cell.deleteButtonOffset = CGPointMake(0, 0);
}
CustomGridViewCell *gridViewContent = [CustomGridViewCell getNewGridViewCellForOrientation:orientation];
ContentData *data = [wishlistContentArray objectAtIndex:index];
[gridViewContent setuprWithContent:data];
cell.contentView = gridViewContent;
if (!gridViewContent.delegate)
gridViewContent.delegate = self;
return cell;
}
There is a bug on GMGridView when it computes the number of visible cells.
In loadRequiredItems there are these two lines of code
NSRange rangeOfPositions = [self.layoutStrategy rangeOfPositionsInBoundsFromOffset: self.contentOffset];
NSRange loadedPositionsRange = NSMakeRange(self.firstPositionLoaded, self.lastPositionLoaded - self.firstPositionLoaded);
You have to change the range so it will return the smallest number that is a multiple of the number of items on your cell.
For example if you have 2 cells on a row, right now the code will return a range of {0,5} but it should be {0,6}.

UICollectionView Performance Problems on performBatchUpdates

We are trying to set up a UICollectionView with a custom layout. The content of each CollectionViewCell will be an image. Over all there will be several thousand images and about 140-150 being visible at one certain time. On an action event potentially all cells will be reorganized in position and size. The goal is to animate all the moving events currently using the performBatchUpdates method. This causes a huge delay time before everything gets animated.
This far we found out that internally the method layoutAttributesForItemAtIndexPath is called for every single cell (several thousand in total). Additionally, the method cellForItemAtIndexPath is called for more cells than can actually be displayed on the screen.
Are there any possibilities to enhance the performance of the animation?
The default UICollectionViewFlowLayout can't really offer the kind of design we want to realize in the app. Here's some of our code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RPDataModel *dm = [RPDataModel sharedInstance]; //Singleton holding some global information
NSArray *plistArray = dm.plistArray; //Array containing the contents of the cells
NSDictionary *dic = plistArray[[indexPath item]];
RPCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CELL" forIndexPath:indexPath];
cell.label.text = [NSString stringWithFormat:#"%#",dic[#"name"]];
cell.layer.borderColor = nil;
cell.layer.borderWidth = 0.0f;
[cell loadAndSetImageInBackgroundWithLocalFilePath:dic[#"path"]]; //custom method realizing asynchronous loading of the image inside of each cell
return cell;
}
The layoutAttributesForElementsInRect iterates over all elements setting layoutAttributes for allthe elements within the rect. The for-statement breaks on the first cell being past the borders defined by the bottom-right corner of the rect:
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray* attributes = [NSMutableArray array];
RPDataModel *dm = [RPDataModel sharedInstance];
for (int i = 0; i < dm.cellCount; i++) {
CGRect cellRect = [self.rp getCell:i]; //self.rp = custom object offering methods to get information about cells; the getCell method returns the rect of a single cell
if (CGRectIntersectsRect(rect, cellRect)) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[dm.relevanceArray[i][#"product"] intValue] inSection:0];
UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attribute.size = cellRect.size;
attribute.center = CGPointMake(cellRect.origin.x + attribute.size.width / 2, cellRect.origin.y + attribute.size.height / 2);
[attributes addObject:attribute];
} else if (cellRect.origin.x > rect.origin.x + rect.size.width && cellRect.origin.y > rect.origin.y + rect.size.height) {
break;
}
}
return attributes;
}
On Layout changes the results are pretty much the same no matter if the number of cells being defined in the layoutAttributesForElementsInRect is limited or not .. Either the system gets the layout attributes for all cells in there if it isn't limited or it calls the layoutAttributesForElementAtIndexPath method for all the missing cells if it is limited. Overall the attributes for every single cell is being used somehow.
-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
RPDataModel *dm = [RPDataModel sharedInstance];
UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGRect cellRect = [self.rp getCell:[dm.indexDictionary[#(indexPath.item)] intValue]];
attribute.size = cellRect.size;
attribute.center = CGPointMake(cellRect.origin.x + attribute.size.width / 2, cellRect.origin.y + attribute.size.height / 2);
return attribute;
}
Without seeing code, my guess is that your layoutAttributesForElementsInRect method is iterating through all of the items in your collection, and that is, in turn, what is causing the other methods to get over-called. layoutAttributesForElementsInRect is giving you a hint - that is, the CGRect it is passing to you - about which items you need to call layoutAttributesForItemAtIndexPath for, in terms of what is on the screen.
So that may be part of the performance issue - that is, tuning your custom layout so it is being smart about which items it is updating.
The other issues pertain to animation performance in general. One thing to watch out for is if there is any sort of compositing going on - make sure your images are opaque. Another thing is if you're using shadows on your image, those can be expensive to animate. One way to improve the animation performance of shadows is set the shadowPath value when the image is resized - if you do have shadows, let me know and post some code for doing that.
This appears to be caused by trying to add cells to sections which have header views but no cells already in them.
The error message is monumentally unhelpful, and it took several hours of effort to trace it down.

Resources