Skip to content

Commit

Permalink
Add automatically computed reuse identifier feature for CollectionVie…
Browse files Browse the repository at this point in the history
…wCell and TableViewCell (#15)

* [AddReuseIdentifiersForTableAndCollectionViewCells] - Finish implementation

* [AddReuseIdentifiersForTableAndCollectionViewCells] - Add unit tests and improve implementation
  • Loading branch information
erincolkan authored Apr 22, 2022
1 parent fffe11a commit 34d7b60
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
28 changes: 28 additions & 0 deletions FCExtensionKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
A01455772812962200E89557 /* UICollectionViewCell+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A01455762812962200E89557 /* UICollectionViewCell+Additions.swift */; };
A01455792812968600E89557 /* UITableViewCell+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A01455782812968600E89557 /* UITableViewCell+Additions.swift */; };
A03050582812B88B0087489C /* FakeCellClasses.swift in Sources */ = {isa = PBXBuildFile; fileRef = A03050572812B88B0087489C /* FakeCellClasses.swift */; };
A030505A2812B8D90087489C /* UITableViewCell+AdditionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A03050592812B8D90087489C /* UITableViewCell+AdditionsTests.swift */; };
A030507E2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A030507D2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift */; };
D06791AE2704736A00E369CF /* Data+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06791AD2704736A00E369CF /* Data+Additions.swift */; };
FE7B494926C482C8009AAB82 /* CodableEnum.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE7B494826C482C8009AAB82 /* CodableEnum.swift */; };
FE7B494D26C485D7009AAB82 /* CodableEnumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE7B494C26C485D7009AAB82 /* CodableEnumTests.swift */; };
Expand Down Expand Up @@ -91,6 +96,11 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
A01455762812962200E89557 /* UICollectionViewCell+Additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UICollectionViewCell+Additions.swift"; sourceTree = "<group>"; };
A01455782812968600E89557 /* UITableViewCell+Additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableViewCell+Additions.swift"; sourceTree = "<group>"; };
A03050572812B88B0087489C /* FakeCellClasses.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeCellClasses.swift; sourceTree = "<group>"; };
A03050592812B8D90087489C /* UITableViewCell+AdditionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableViewCell+AdditionsTests.swift"; sourceTree = "<group>"; };
A030507D2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UICollectionViewCell+AdditionsTests.swift"; sourceTree = "<group>"; };
D06791AD2704736A00E369CF /* Data+Additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Additions.swift"; sourceTree = "<group>"; };
FE7B494826C482C8009AAB82 /* CodableEnum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodableEnum.swift; sourceTree = "<group>"; };
FE7B494C26C485D7009AAB82 /* CodableEnumTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodableEnumTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -186,6 +196,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
A03050562812B8670087489C /* Utilities */ = {
isa = PBXGroup;
children = (
A03050572812B88B0087489C /* FakeCellClasses.swift */,
);
path = Utilities;
sourceTree = "<group>";
};
FEF297AA264DC78300E50ACB = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -290,6 +308,8 @@
FEF297FB264DC81900E50ACB /* UIScrollView+Additions.swift */,
FEF297FC264DC81900E50ACB /* UITableView+Additions.swift */,
FEF297FD264DC81900E50ACB /* UICollectionView+Additions.swift */,
A01455762812962200E89557 /* UICollectionViewCell+Additions.swift */,
A01455782812968600E89557 /* UITableViewCell+Additions.swift */,
);
path = UIKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -342,6 +362,7 @@
FEF29832264DC85200E50ACB /* UIKit */ = {
isa = PBXGroup;
children = (
A03050562812B8670087489C /* Utilities */,
FEF29833264DC85200E50ACB /* UIColor+AdditionsTests.swift */,
FEF29834264DC85200E50ACB /* Helpers.swift */,
FEF29835264DC85200E50ACB /* UIViewController+AdditionsTests.swift */,
Expand All @@ -350,6 +371,8 @@
FEF29838264DC85200E50ACB /* UIView+AdditionsTests.swift */,
FEF29839264DC85200E50ACB /* UIApplication+AdditionsTests.swift */,
FEF2983A264DC85200E50ACB /* UIEdgeInsets+AdditionsTests.swift */,
A03050592812B8D90087489C /* UITableViewCell+AdditionsTests.swift */,
A030507D2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift */,
);
path = UIKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -507,6 +530,7 @@
FEF29821264DC81900E50ACB /* UIScrollView+Additions.swift in Sources */,
FEF2980E264DC81900E50ACB /* Collection+Additions.swift in Sources */,
FEF2981F264DC81900E50ACB /* UILabel+Additions.swift in Sources */,
A01455772812962200E89557 /* UICollectionViewCell+Additions.swift in Sources */,
FEF2980B264DC81900E50ACB /* NSMutableAttributedString+Additions.swift in Sources */,
FEF29813264DC81900E50ACB /* String+Additions.swift in Sources */,
FEF29825264DC81900E50ACB /* WebPProcessor.swift in Sources */,
Expand All @@ -529,6 +553,7 @@
FEF29807264DC81900E50ACB /* NSNumber+Additions.swift in Sources */,
FEF29823264DC81900E50ACB /* UICollectionView+Additions.swift in Sources */,
FEF29811264DC81900E50ACB /* CountableClosedRage+Additions.swift in Sources */,
A01455792812968600E89557 /* UITableViewCell+Additions.swift in Sources */,
FEF29818264DC81900E50ACB /* UIImage+Additions.swift in Sources */,
FEF29808264DC81900E50ACB /* Date+Additions.swift in Sources */,
FEF29812264DC81900E50ACB /* Bool+Additions.swift in Sources */,
Expand All @@ -555,11 +580,14 @@
FEF2986C264DC90900E50ACB /* Int+AdditionsTests.swift in Sources */,
FEF29873264DCA2000E50ACB /* CGRect+AdditionsTests.swift in Sources */,
FEF29890264DCA5400E50ACB /* UIApplication+AdditionsTests.swift in Sources */,
A030507E2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift in Sources */,
FEF29874264DCA2400E50ACB /* URL+AdditionsTests.swift in Sources */,
FEF29875264DCA2700E50ACB /* NSString+AdditionsTests.swift in Sources */,
FEF29877264DCA2E00E50ACB /* IndexPath+AdditionsTests.swift in Sources */,
FEF2988D264DCA5400E50ACB /* UIEdgeInsets+AdditionsTests.swift in Sources */,
A030505A2812B8D90087489C /* UITableViewCell+AdditionsTests.swift in Sources */,
FEF29871264DCA1A00E50ACB /* CGSize+AdditionsTests.swift in Sources */,
A03050582812B88B0087489C /* FakeCellClasses.swift in Sources */,
FEF2988E264DCA5400E50ACB /* Collection+AdditionsTests.swift in Sources */,
FEF2986E264DCA0300E50ACB /* UIImage+AdditionsTests.swift in Sources */,
FEF29879264DCA3400E50ACB /* NSMutableAttributedString+AdditionsTests.swift in Sources */,
Expand Down
20 changes: 20 additions & 0 deletions Sources/UIKit/UICollectionViewCell+Additions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// UICollectionViewCell+Additions.swift
// FCExtensionKit
//
// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022.
//

import UIKit

extension UICollectionViewCell {
/// Automatically changes reuse identifier property of an xib designed cell.
open override var reuseIdentifier: String {
return String(describing: Self.self)
}

/// Creates a static reuse identifier for collection view cell using its class name.
public static var reuseIdentifier: String {
return String(describing: Self.self)
}
}
17 changes: 17 additions & 0 deletions Sources/UIKit/UITableViewCell+Additions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UITableViewCell+Additions.swift
// FCExtensionKit
//
// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022.
//

import UIKit

public extension UITableViewCell {
/// Creates a static reuse identifier for table view cell using its class name.
///
/// - Warning: If you are using xib files for your cell design, you are obliged to manually assign a reuse identifier for your cell in the Interface Builder.
static var reuseIdentifier: String {
return String(describing: self)
}
}
37 changes: 37 additions & 0 deletions Tests/UIKit/UICollectionViewCell+AdditionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// UICollectionViewCell+AdditionsTests.swift
// FCExtensionKitTests
//
// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022.
//

import XCTest
import UIKit
@testable import FCExtensionKit

class UICollectionViewCell_AdditionsTests: XCTestCase {
var collectionView: UICollectionView!

override func setUp() {
super.setUp()

let layout = UICollectionViewLayout()
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.register(FakeCollectionViewCell.self, forCellWithReuseIdentifier: FakeCollectionViewCell.reuseIdentifier)
}

func testCanDequeueCellUsingInstanceVariableReuseIdentifier() {
let cellInstance = FakeCollectionViewCell()
let dequeuedCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellInstance.reuseIdentifier, for: IndexPath(row: 0, section: 0)) as? FakeCollectionViewCell

XCTAssertNotNil(dequeuedCell)
XCTAssertEqual(dequeuedCell?.reuseIdentifier, FakeCollectionViewCell.reuseIdentifier)
}

func testCanDequeueCellUsingStaticReuseIdentifier() {
let dequeuedCell = collectionView.dequeueReusableCell(withReuseIdentifier: FakeCollectionViewCell.reuseIdentifier, for: IndexPath(row: 0, section: 0)) as? FakeCollectionViewCell

XCTAssertNotNil(dequeuedCell)
XCTAssertEqual(dequeuedCell?.reuseIdentifier, FakeCollectionViewCell.reuseIdentifier)
}
}
28 changes: 28 additions & 0 deletions Tests/UIKit/UITableViewCell+AdditionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// UITableViewCell+AdditionsTests.swift
// FCExtensionKitTests
//
// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022.
//

import XCTest
import UIKit
@testable import FCExtensionKit

class UITableViewCell_AdditionsTests: XCTestCase {
var tableView: UITableView!

override func setUp() {
super.setUp()

tableView = UITableView()
tableView.register(FakeTableViewCell.self, forCellReuseIdentifier: FakeTableViewCell.reuseIdentifier)
}

func testCanDequeueCellUsingReuseIdentifier() {
let cell = tableView.dequeueReusableCell(withIdentifier: FakeTableViewCell.reuseIdentifier) as? FakeTableViewCell

XCTAssertNotNil(cell)
XCTAssertEqual(cell?.reuseIdentifier, FakeTableViewCell.reuseIdentifier)
}
}
23 changes: 23 additions & 0 deletions Tests/UIKit/Utilities/FakeCellClasses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// FakeCellClasses.swift
// FCExtensionKitTests
//
// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022.
//

import UIKit
import FCExtensionKit

class FakeCollectionViewCell: UICollectionViewCell {

}

class FakeTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle = .default, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

0 comments on commit 34d7b60

Please sign in to comment.