Sunday, January 24, 2016

Objective C UICollectionView Layouts

We know that for a list data we have UITableView, UIScrollView for display purpose.

Using UIScrollview, we can display both in horizontal and vertical directions. Whereas, using UITableView, we can only display list data vertical direction. Having said that, populating list data in UIScrollView needs loads of mathematical calculations for arranging data items (Images, views, labels etc) properly with alignment.

UICollectionView is the best choice If you are populating a list data and you need to change the direction of scroll even at run time. It is very handy compared to UITableView and UIScrollView.


                         



Unlike the UITableView and UIScrollview, UICollectionView needs a layout at the time of initialization. In this layout we tell the direction of the data display and all. If we don't set a non nil layout parameter, You will end up with a crash throwing the below error.

"UICollectionView must be initialized with a non-nil layout parameter"

If you are placing the UICollectionView control in the XIB and giving the connection, There you will be having to set the layout parameter.

There are two types of layouts available.

  • Flow
  • Custom


Flow is the default layout as shown below.




  UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];      
       
  [collectionViewLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
  UICollectionView  *collectionView = [[UICollectionView alloc]       
            initWithFrame:CGRectMake(0,0,200,300) collectionViewLayout:collectionViewLayout];  

    

We can also subclass UICollectionView layout and give it as custom class in the XIB or as shown below when initializing programatically.




  HomeCollectionViewFlowLayout *kCollViewLayout = [[HomeCollectionViewFlowLayout alloc]init];
                                                              
  [kCollViewLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
  UICollectionView  *collectionView = 
             [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,200,300) kCollViewLayout];     
                     



Here, HomeCollectionViewFlowLayout is my custom class which I subclassed from UICollectionViewFlowLayout class.

Hope this post is useful. Feel free to comment incase of any queries.

Swift Protocols & Extensions
Swift Comparable Protocol
Swift Equatable Protocol
Protocol Extensions for Default and Convenient API
Protocols vs Base classes
Multiple Inheritance using protocols
Swift Protocol Extensions
Swift Constrained Extensions
Multiple Inheritance, Diamond problem
Multiple Inheritance using protocols, Diamond Problem
Swift - Preserving Structure Default Initializer using Extensions
Swift Closures
Swift Closures Capture List
Swift Escaping Closures (@escaping)
Swift - weak and unowned references
Swift 4 Tutorials
Swift 4 NEW Tutorial-1 (One Sided Ranges)
Swift 4 NEW Tutorial-2 (Strings are Collections Again)
Swift 4 NEW Tutorial-3 (private instead fileprivate)
Swift 4 NEW Tutorial-4 (fileprivate to private)
Swift CoreData Tutorials
Swift - CoreData - CRUD Operations (Tutorial-1)
Swift - CoreData - Validations (Tutorial-2)
Swift - CoreData - To One Relationship (Tutorial-3)
Swift - CoreData - To One - Inverse Relationship (Tutorial-4)
Swift - CoreData - To Many - Inverse Relationship (Tutorial-5)
Method Swizzling Tutorials
Swift 4, Method initailize defines Objective-C class method initialize(), which is not guaranteed to be invoked by Swift and will be disallowed in future versions
Swift 4 Method Swizzling (Part 2/2)
Swift 4 Method Swizzling (Part 1/2)
NSPredicate Tutorials
Objective C NSPredicate Part 1/4
Objective C NSPredicate Part 2/4
Objective C NSPredicate Part 3/4
Objective C NSPredicate Part 4/4
iOS Fixing Security Vulerabilities Tutorials
iOS Format String Attacks
iOS Jailbreak Unchecked File Operation Result Code
iOS Screen Caching Security Vulnerability
SharePoint API Tutorials
Objective-C Upload File SharerPoint Soap Service
Objective-C Create Folder SharerPoint Soap Service
Objective-C Renaming Folder or File of a SharePoint content
Objective C SharePoint content fetch soap service
Objective C SharePoint login service
Objective-C SharePoint User Permissions
Objective-C SharePoint User Profile Service
Objective-C SharePoint Service For Deleting An Item
Objective-C SharePoint Authentication Mode
XMPP Chat Framework Tutorials
Encoding & Decoding emoji characters In the chat applications using Objective C
Objective C methods for new user registration in xmpp framework
Objective-C XMPP search users by username
Retrieving chat history from Openfire using XMPP
Displaying emoji characters in UILabel
Getting old messages or chat history from XMPP
XMPP searching users by username
XMPP framework User Registration Methods
XMPP user change password using Objective C
Change XMPP registered user password
Blog Archive
Custom cell class data source in MVVM design pattern
Swift - Multiple levels of Optional Binding
Swift - App is currently in Main Thread or not
Swift Nil-Coalescing Operator for Optionals
Enum raw value comparisons
guard statement return/break/continue
Case-sensitive and Case-Insensitive sorting
valueForKey (vs) objectForKey
If-Let & Guard-Let Multiple Optional Bindings
Color and Image Literals
Swift Enums as Models
Swift convenience initializers
Tappable links in UITextView
UITableView Scrolling issue for cells having UITextField/UITextView as subviews
illegal Configuration Safe Area Layout Guide before iOS 9.0
iOS Checking device is in 24 hour format or not
Displaying URL loading error in WebView in HTML format
Checking whether iOS app is running in a device having safe area (iPhoneX) or not
xcodebuild: error: invalid option '-exportFormat'
Interface Segregation Principle in SOLID (Object-Oriented Design)
Swift - Handling web links in dynamic content
Swift - Class and Static Methods
Swift - MVVM - RXSwift Instead of didSet()
Swift Failable Initializers
Objective-C/Swift avoid crashes in iPad in an iPhone Application

1 comment: