├── .gitignore ├── Collection View in a Table View Cell.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── License.md ├── README.md └── Table View in a Collection View ├── AppDelegate.swift ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Helpers.swift ├── Info.plist ├── TableViewCell.swift └── ViewController.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /Collection View in a Table View Cell.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 5EC197A91BE90648006F33C7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EC197A81BE90648006F33C7 /* AppDelegate.swift */; }; 11 | 5EC197AB1BE90648006F33C7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EC197AA1BE90648006F33C7 /* ViewController.swift */; }; 12 | 5EC197AE1BE90648006F33C7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5EC197AC1BE90648006F33C7 /* Main.storyboard */; }; 13 | 5EC197B01BE90648006F33C7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5EC197AF1BE90648006F33C7 /* Assets.xcassets */; }; 14 | 5EC197B31BE90648006F33C7 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5EC197B11BE90648006F33C7 /* LaunchScreen.storyboard */; }; 15 | 5EC197BB1BE90736006F33C7 /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EC197BA1BE90736006F33C7 /* TableViewCell.swift */; }; 16 | 5EC197DF1BE91DD2006F33C7 /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EC197DE1BE91DD2006F33C7 /* Helpers.swift */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 5EC197A51BE90648006F33C7 /* Collection View in a Table View Cell.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Collection View in a Table View Cell.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 5EC197A81BE90648006F33C7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 22 | 5EC197AA1BE90648006F33C7 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 23 | 5EC197AD1BE90648006F33C7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 24 | 5EC197AF1BE90648006F33C7 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | 5EC197B21BE90648006F33C7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 26 | 5EC197B41BE90648006F33C7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | 5EC197BA1BE90736006F33C7 /* TableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewCell.swift; sourceTree = ""; }; 28 | 5EC197DE1BE91DD2006F33C7 /* Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | 5EC197A21BE90648006F33C7 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | 5EC1979C1BE90648006F33C7 = { 43 | isa = PBXGroup; 44 | children = ( 45 | 5EC197A71BE90648006F33C7 /* Table View in a Collection View */, 46 | 5EC197A61BE90648006F33C7 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | 5EC197A61BE90648006F33C7 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | 5EC197A51BE90648006F33C7 /* Collection View in a Table View Cell.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | 5EC197A71BE90648006F33C7 /* Table View in a Collection View */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 5EC197A81BE90648006F33C7 /* AppDelegate.swift */, 62 | 5EC197AA1BE90648006F33C7 /* ViewController.swift */, 63 | 5EC197BA1BE90736006F33C7 /* TableViewCell.swift */, 64 | 5EC197DE1BE91DD2006F33C7 /* Helpers.swift */, 65 | 5EC197AC1BE90648006F33C7 /* Main.storyboard */, 66 | 5EC197AF1BE90648006F33C7 /* Assets.xcassets */, 67 | 5EC197B11BE90648006F33C7 /* LaunchScreen.storyboard */, 68 | 5EC197B41BE90648006F33C7 /* Info.plist */, 69 | ); 70 | path = "Table View in a Collection View"; 71 | sourceTree = ""; 72 | }; 73 | /* End PBXGroup section */ 74 | 75 | /* Begin PBXNativeTarget section */ 76 | 5EC197A41BE90648006F33C7 /* Collection View in a Table View Cell */ = { 77 | isa = PBXNativeTarget; 78 | buildConfigurationList = 5EC197B71BE90648006F33C7 /* Build configuration list for PBXNativeTarget "Collection View in a Table View Cell" */; 79 | buildPhases = ( 80 | 5EC197A11BE90648006F33C7 /* Sources */, 81 | 5EC197A21BE90648006F33C7 /* Frameworks */, 82 | 5EC197A31BE90648006F33C7 /* Resources */, 83 | ); 84 | buildRules = ( 85 | ); 86 | dependencies = ( 87 | ); 88 | name = "Collection View in a Table View Cell"; 89 | productName = "Table View in a Collection View"; 90 | productReference = 5EC197A51BE90648006F33C7 /* Collection View in a Table View Cell.app */; 91 | productType = "com.apple.product-type.application"; 92 | }; 93 | /* End PBXNativeTarget section */ 94 | 95 | /* Begin PBXProject section */ 96 | 5EC1979D1BE90648006F33C7 /* Project object */ = { 97 | isa = PBXProject; 98 | attributes = { 99 | LastSwiftUpdateCheck = 0710; 100 | LastUpgradeCheck = 0810; 101 | ORGANIZATIONNAME = "Ash Furrow"; 102 | TargetAttributes = { 103 | 5EC197A41BE90648006F33C7 = { 104 | CreatedOnToolsVersion = 7.1; 105 | LastSwiftMigration = 0810; 106 | }; 107 | }; 108 | }; 109 | buildConfigurationList = 5EC197A01BE90648006F33C7 /* Build configuration list for PBXProject "Collection View in a Table View Cell" */; 110 | compatibilityVersion = "Xcode 3.2"; 111 | developmentRegion = English; 112 | hasScannedForEncodings = 0; 113 | knownRegions = ( 114 | English, 115 | en, 116 | Base, 117 | ); 118 | mainGroup = 5EC1979C1BE90648006F33C7; 119 | productRefGroup = 5EC197A61BE90648006F33C7 /* Products */; 120 | projectDirPath = ""; 121 | projectRoot = ""; 122 | targets = ( 123 | 5EC197A41BE90648006F33C7 /* Collection View in a Table View Cell */, 124 | ); 125 | }; 126 | /* End PBXProject section */ 127 | 128 | /* Begin PBXResourcesBuildPhase section */ 129 | 5EC197A31BE90648006F33C7 /* Resources */ = { 130 | isa = PBXResourcesBuildPhase; 131 | buildActionMask = 2147483647; 132 | files = ( 133 | 5EC197B31BE90648006F33C7 /* LaunchScreen.storyboard in Resources */, 134 | 5EC197B01BE90648006F33C7 /* Assets.xcassets in Resources */, 135 | 5EC197AE1BE90648006F33C7 /* Main.storyboard in Resources */, 136 | ); 137 | runOnlyForDeploymentPostprocessing = 0; 138 | }; 139 | /* End PBXResourcesBuildPhase section */ 140 | 141 | /* Begin PBXSourcesBuildPhase section */ 142 | 5EC197A11BE90648006F33C7 /* Sources */ = { 143 | isa = PBXSourcesBuildPhase; 144 | buildActionMask = 2147483647; 145 | files = ( 146 | 5EC197BB1BE90736006F33C7 /* TableViewCell.swift in Sources */, 147 | 5EC197AB1BE90648006F33C7 /* ViewController.swift in Sources */, 148 | 5EC197A91BE90648006F33C7 /* AppDelegate.swift in Sources */, 149 | 5EC197DF1BE91DD2006F33C7 /* Helpers.swift in Sources */, 150 | ); 151 | runOnlyForDeploymentPostprocessing = 0; 152 | }; 153 | /* End PBXSourcesBuildPhase section */ 154 | 155 | /* Begin PBXVariantGroup section */ 156 | 5EC197AC1BE90648006F33C7 /* Main.storyboard */ = { 157 | isa = PBXVariantGroup; 158 | children = ( 159 | 5EC197AD1BE90648006F33C7 /* Base */, 160 | ); 161 | name = Main.storyboard; 162 | sourceTree = ""; 163 | }; 164 | 5EC197B11BE90648006F33C7 /* LaunchScreen.storyboard */ = { 165 | isa = PBXVariantGroup; 166 | children = ( 167 | 5EC197B21BE90648006F33C7 /* Base */, 168 | ); 169 | name = LaunchScreen.storyboard; 170 | sourceTree = ""; 171 | }; 172 | /* End PBXVariantGroup section */ 173 | 174 | /* Begin XCBuildConfiguration section */ 175 | 5EC197B51BE90648006F33C7 /* Debug */ = { 176 | isa = XCBuildConfiguration; 177 | buildSettings = { 178 | ALWAYS_SEARCH_USER_PATHS = NO; 179 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 180 | CLANG_CXX_LIBRARY = "libc++"; 181 | CLANG_ENABLE_MODULES = YES; 182 | CLANG_ENABLE_OBJC_ARC = YES; 183 | CLANG_WARN_BOOL_CONVERSION = YES; 184 | CLANG_WARN_CONSTANT_CONVERSION = YES; 185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 186 | CLANG_WARN_EMPTY_BODY = YES; 187 | CLANG_WARN_ENUM_CONVERSION = YES; 188 | CLANG_WARN_INFINITE_RECURSION = YES; 189 | CLANG_WARN_INT_CONVERSION = YES; 190 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 191 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 192 | CLANG_WARN_UNREACHABLE_CODE = YES; 193 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 194 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 195 | COPY_PHASE_STRIP = NO; 196 | DEBUG_INFORMATION_FORMAT = dwarf; 197 | ENABLE_STRICT_OBJC_MSGSEND = YES; 198 | ENABLE_TESTABILITY = YES; 199 | GCC_C_LANGUAGE_STANDARD = gnu99; 200 | GCC_DYNAMIC_NO_PIC = NO; 201 | GCC_NO_COMMON_BLOCKS = YES; 202 | GCC_OPTIMIZATION_LEVEL = 0; 203 | GCC_PREPROCESSOR_DEFINITIONS = ( 204 | "DEBUG=1", 205 | "$(inherited)", 206 | ); 207 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 208 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 209 | GCC_WARN_UNDECLARED_SELECTOR = YES; 210 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 211 | GCC_WARN_UNUSED_FUNCTION = YES; 212 | GCC_WARN_UNUSED_VARIABLE = YES; 213 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 214 | MTL_ENABLE_DEBUG_INFO = YES; 215 | ONLY_ACTIVE_ARCH = YES; 216 | SDKROOT = iphoneos; 217 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 218 | SWIFT_VERSION = 4.0; 219 | }; 220 | name = Debug; 221 | }; 222 | 5EC197B61BE90648006F33C7 /* Release */ = { 223 | isa = XCBuildConfiguration; 224 | buildSettings = { 225 | ALWAYS_SEARCH_USER_PATHS = NO; 226 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 227 | CLANG_CXX_LIBRARY = "libc++"; 228 | CLANG_ENABLE_MODULES = YES; 229 | CLANG_ENABLE_OBJC_ARC = YES; 230 | CLANG_WARN_BOOL_CONVERSION = YES; 231 | CLANG_WARN_CONSTANT_CONVERSION = YES; 232 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 233 | CLANG_WARN_EMPTY_BODY = YES; 234 | CLANG_WARN_ENUM_CONVERSION = YES; 235 | CLANG_WARN_INFINITE_RECURSION = YES; 236 | CLANG_WARN_INT_CONVERSION = YES; 237 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 238 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 239 | CLANG_WARN_UNREACHABLE_CODE = YES; 240 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 241 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 242 | COPY_PHASE_STRIP = NO; 243 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 244 | ENABLE_NS_ASSERTIONS = NO; 245 | ENABLE_STRICT_OBJC_MSGSEND = YES; 246 | GCC_C_LANGUAGE_STANDARD = gnu99; 247 | GCC_NO_COMMON_BLOCKS = YES; 248 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 249 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 250 | GCC_WARN_UNDECLARED_SELECTOR = YES; 251 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 252 | GCC_WARN_UNUSED_FUNCTION = YES; 253 | GCC_WARN_UNUSED_VARIABLE = YES; 254 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 255 | MTL_ENABLE_DEBUG_INFO = NO; 256 | SDKROOT = iphoneos; 257 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 258 | SWIFT_VERSION = 4.0; 259 | VALIDATE_PRODUCT = YES; 260 | }; 261 | name = Release; 262 | }; 263 | 5EC197B81BE90648006F33C7 /* Debug */ = { 264 | isa = XCBuildConfiguration; 265 | buildSettings = { 266 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 267 | INFOPLIST_FILE = "Table View in a Collection View/Info.plist"; 268 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 269 | PRODUCT_BUNDLE_IDENTIFIER = "com.ashfurrow.Table-View-in-a-Collection-View"; 270 | PRODUCT_NAME = "Collection View in a Table View Cell"; 271 | SWIFT_VERSION = 4.0; 272 | }; 273 | name = Debug; 274 | }; 275 | 5EC197B91BE90648006F33C7 /* Release */ = { 276 | isa = XCBuildConfiguration; 277 | buildSettings = { 278 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 279 | INFOPLIST_FILE = "Table View in a Collection View/Info.plist"; 280 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 281 | PRODUCT_BUNDLE_IDENTIFIER = "com.ashfurrow.Table-View-in-a-Collection-View"; 282 | PRODUCT_NAME = "Collection View in a Table View Cell"; 283 | SWIFT_VERSION = 4.0; 284 | }; 285 | name = Release; 286 | }; 287 | /* End XCBuildConfiguration section */ 288 | 289 | /* Begin XCConfigurationList section */ 290 | 5EC197A01BE90648006F33C7 /* Build configuration list for PBXProject "Collection View in a Table View Cell" */ = { 291 | isa = XCConfigurationList; 292 | buildConfigurations = ( 293 | 5EC197B51BE90648006F33C7 /* Debug */, 294 | 5EC197B61BE90648006F33C7 /* Release */, 295 | ); 296 | defaultConfigurationIsVisible = 0; 297 | defaultConfigurationName = Release; 298 | }; 299 | 5EC197B71BE90648006F33C7 /* Build configuration list for PBXNativeTarget "Collection View in a Table View Cell" */ = { 300 | isa = XCConfigurationList; 301 | buildConfigurations = ( 302 | 5EC197B81BE90648006F33C7 /* Debug */, 303 | 5EC197B91BE90648006F33C7 /* Release */, 304 | ); 305 | defaultConfigurationIsVisible = 0; 306 | defaultConfigurationName = Release; 307 | }; 308 | /* End XCConfigurationList section */ 309 | }; 310 | rootObject = 5EC1979D1BE90648006F33C7 /* Project object */; 311 | } 312 | -------------------------------------------------------------------------------- /Collection View in a Table View Cell.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Collection View in a Table View Cell.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ash Furrow 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Putting a collection view inside a table view cell has become a common user interface pattern. This repository contains sample code for a [tutorial where I show you how to do just that](http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/). 2 | 3 |

4 | 5 |

6 | 7 | This code is licensed under MIT, so you can do whatever you like with it. If you have a question, just [open an issue](https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell/issues/new) :tada: 8 | -------------------------------------------------------------------------------- /Table View in a Collection View/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | @UIApplicationMain 4 | class AppDelegate: UIResponder, UIApplicationDelegate { 5 | 6 | var window: UIWindow? 7 | 8 | 9 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 10 | 11 | return true 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Table View in a Collection View/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /Table View in a Collection View/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Table View in a Collection View/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /Table View in a Collection View/Helpers.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | func generateRandomData() -> [[UIColor]] { 4 | let numberOfRows = 20 5 | let numberOfItemsPerRow = 15 6 | 7 | return (0.. UIColor { 15 | 16 | let hue = CGFloat(arc4random() % 100) / 100 17 | let saturation = CGFloat(arc4random() % 100) / 100 18 | let brightness = CGFloat(arc4random() % 100) / 100 19 | 20 | return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Table View in a Collection View/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Table View in a Collection View/TableViewCell.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | class TableViewCell: UITableViewCell { 4 | 5 | @IBOutlet fileprivate weak var collectionView: UICollectionView! 6 | 7 | } 8 | 9 | extension TableViewCell { 10 | 11 | func setCollectionViewDataSourceDelegate(_ dataSourceDelegate: D, forRow row: Int) { 12 | 13 | collectionView.delegate = dataSourceDelegate 14 | collectionView.dataSource = dataSourceDelegate 15 | collectionView.tag = row 16 | collectionView.setContentOffset(collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling. 17 | collectionView.reloadData() 18 | } 19 | 20 | var collectionViewOffset: CGFloat { 21 | set { collectionView.contentOffset.x = newValue } 22 | get { return collectionView.contentOffset.x } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Table View in a Collection View/ViewController.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | class ViewController: UITableViewController { 4 | 5 | let model = generateRandomData() 6 | var storedOffsets = [Int: CGFloat]() 7 | 8 | override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 9 | return model.count 10 | } 11 | 12 | override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 13 | 14 | let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 15 | 16 | return cell 17 | } 18 | 19 | override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 20 | 21 | guard let tableViewCell = cell as? TableViewCell else { return } 22 | 23 | tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) 24 | tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0 25 | } 26 | 27 | override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 28 | 29 | guard let tableViewCell = cell as? TableViewCell else { return } 30 | 31 | storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset 32 | } 33 | } 34 | 35 | extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource { 36 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 37 | return model[collectionView.tag].count 38 | } 39 | 40 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 41 | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 42 | 43 | cell.backgroundColor = model[collectionView.tag][indexPath.item] 44 | 45 | return cell 46 | } 47 | 48 | func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 49 | print("Collection view at row \(collectionView.tag) selected index path \(indexPath)") 50 | } 51 | } 52 | --------------------------------------------------------------------------------