Last updated on: May 27, 2023
I often caught myself writing the same code repeatedly when I wanted to add a UITableView or a UICollectionView in a UIViewController. You know… all these methods from Delegates and DataSources…
I recently found a way to have all this code written from the beginning every time you create a new file, and it’s called File Templates.
In this tutorial, I’ll show an example of how you can create a UIViewController File Template with a UITableView and all the required methods ready to go for the next time.
Creating a custom File Template
Open the Terminal and paste the following line to create a folder named ‘Custom‘ inside Xcode.
mkdir -p ~/Library/Developer/Xcode/Templates/File\ Templates/Custom
Next, open Finder, press Shift +Command + G and paste the following path:
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates
Then, go to Source and copy the folder Package Swift File.xctemplate somewhere on your Desktop
After that, on Finder press Shift + Command + G again and paste the following:
~/Library/Developer/Xcode/Templates
Inside the Custom folder, paste the Package Swift File.xctemplate you copied before on your Desktop.
Now, change the name of the Package Swift File.xctemplate folder to a different name, for example My TableView Controller.xctemplate
If you open the __FILEBASENAME__.swift you’ll see the following:
Inside here we’re going to write the code that we want our template to have when we create our file.
As you start typing you’ll see that there’s no autocomplete, and it’s difficult to write everything like that.
So, open an Xcode project and create a simple UIViewController and write the code you want there, and then paste it inside the __FILEBASENAME__.swift like that:
import UIKit
class ___FILEBASENAME___: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
initView()
initViewModel()
}
func initView(){
tableView.delegate = self
tableView.dataSource = self
}
func initViewModel() {
}
}
extension ___FILEBASENAME___: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}
extension ___FILEBASENAME___: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
}
Code language: Swift (swift)
Don’t forget to change the name of the class to __FILEBASENAME__ so when you create the file, the class will get the same name as your file.
After you finish that, open the TemplateInfo.plist and change the following:
- HiddenFromLibrary: set it to 0
- HiddenFromChooser: set it to 0
and save it.
Now, when you going to create a new File, you see something like the following image:
You can find the files here
If you have any questions, please feel free to leave a comment below