iOS Tips: UICollectionView

1. old image flashes before showing new one while transfer from one UIViewController to another one

solution: do scrolling before push from navigation controller or present next UIViewController

2. NSURLErrorDomain 999

solution: most posts suggest to ignore the error, but it will affect the flow and try to reduce unnecessary request as much as possible, even async ones

3. Pull up to refresh library MJRefresh stop loading more

reason:

collectionview.footer.hidden = YES

blocks triggering loading more

solution: remove it and works fine

reference: https://github.com/CoderMJLee/MJRefresh/issues/246

4. Sometimes UICollectionVIewCell hides after return from other View Controller, reload the visible cells can solve it and keep the same position in the UICollectionView

if (_options < 3) {
    // shows black screen (images are there but not visible, even not hidden) if not reload
    [_myCollectionView reloadItemsAtIndexPaths:_myCollectionView.indexPathsForVisibleItems];
}
 5. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

8. Adjust collection view cell based on UILabel inside (dynamic UICollectionViewCell height)

In function
– (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
use
CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:label.font}
                                                  context:context].size;
get actual size to fit string text in UILabel and then add the base height to generate collection view cell height dynamically.
Dynamic UILabel width:
Auto Layout width <= threshold (217) and leading/trailing constraint

 

To be continue …

Advertisements

About liyao13

Yao Li is a web and iOS developer, blogger and he has a passion for technology and business. In his blogs, he shares code snippets, tutorials, resources and notes to help people develop their skills. Donate $5 to him for a coffee with PayPal at About Me page and read more professional and interesting technical blog articles. Follow him @Yaoli0615 at Twitter to get latest tech updates.
This entry was posted in iOS and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s