UITableViewDiffableDataSource
No description available
Install / Use
/learn @YamamotoDesu/UITableViewDiffableDataSourceREADME
UITableViewDiffableDataSource-Swift
Context
A diffable data source object is a specialized type of data source that works together with your table view object. It provides the behavior you need to manage updates to your table view’s data and UI in a simple, efficient way.
https://developer.apple.com/documentation/uikit/uitableviewdiffabledatasource
Requirement
- Xcode Version 12.4 (12D4e)
- Swift 5
- iOS 13 and onward
- Google Font(Nunito Font: https://fonts.google.com/specimen/Nunito)
iPhone Image
<table border="0"> <tr> <tr> <th>Gif image</th> <th>Standard</th> <th>Dark</th> </tr> <td><img src="https://github.com/YamamotoDesu/UITableViewDiffableDataSource-Swift/blob/main/Gif/RocketSim%20Recording%20-%20iPhone%2012%20-%202021-08-02%2018.18.04.gif" width="300"></td> <td><img src="https://user-images.githubusercontent.com/47273077/126889714-397a476a-7154-4023-947b-411e753a5e5d.png" width="300"></td> <td><img src="https://user-images.githubusercontent.com/47273077/126889687-a6773867-d972-41cb-88c3-20463e7e833a.png" width="300"></td> </tr> </table>iPad Image
<table border="0"> <tr> <tr> <th>Standard</th> <th>Dark</th> </tr> <td><img src="https://user-images.githubusercontent.com/47273077/126889504-a57cef79-78b6-40ac-914c-335af1b73edf.png" width="600"></td> <td><img src="https://user-images.githubusercontent.com/47273077/126889569-7821efaa-9f7f-41bc-aa75-87f074b11fb2.png" width="600"></td> </tr> </table> <table border="0">Sourcecode
Modern UITable View
To connect a diffable data source to a table view(iOS 13 and onward)
lazy var dataSource = configureDataSource()
override func viewDidLoad() {
super.viewDidLoad()
//Assign the diffable data source to your table view.
tableView.dataSource = dataSource
//Generate the current state of the table data by creating a snapshot
var snapshot = NSDiffableDataSourceSnapshot<Section, String>()
snapshot.appendSections([.all])
snapshot.appendItems(restaurantNames, toSection: .all)
//Call the apply() function of the data source to populate the data
dataSource.apply(snapshot, animatingDifferences: false)
}
func configureDataSource() -> UITableViewDiffableDataSource<Section, String> {
let cellIdentifier = "favoritecell"
//Create a UITableViewDiffableDataSource object to connect with your table andprovide the configuration of the table view cells.
let dataSource = UITableViewDiffableDataSource<Section, String>(
tableView: tableView,
cellProvider: { tableView, indexPath, restaurantName in
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
cell.nameLabel.text = restaurantName
cell.thumbnailImageView.image = UIImage(named: self.restaurantImages[indexPath.row])
cell.locationLabel.text = self.restaurantLocations[indexPath.row]
cell.typeLabel.text = self.restaurantTypes[indexPath.row]
cell.heartMark.isHidden = !self.restaurantIsFavorites[indexPath.row]
cell.heartMark.tintColor = UITraitCollection.isDarkMode ? .systemYellow : .blue
return cell
}
)
return dataSource
}
Functionality
Routing With MapKit
https://github.com/YamamotoDesu/UITableViewDiffableDataSource-Swift/blob/main/FoodPin/Controller/MapViewController.swift
<table border="0"> <tr> <tr> <th>Locating</th> <th>Routing</th> </tr> <td><img src="https://user-images.githubusercontent.com/47273077/127778176-1ff08ad5-b5fb-4ad2-a837-97e24a551be6.png" width="300"></td> <td><img src="https://user-images.githubusercontent.com/47273077/127778215-fd130ff8-f72a-4565-a9de-94d564b94f99.png" width="300"></td> </tr> </table>Animation
<table border="0"> <tr> <tr> <th>StandardAnimation</th> <th>SpringAnimation</th> </tr> <td><img src="https://github.com/YamamotoDesu/UITableViewDiffableDataSource-Swift/blob/main/Gif/RocketSim%20Recording%20-%20iPhone%2012%20-%202021-08-02%2012.38.04.gif" width="300"></td> <td><img src="https://github.com/YamamotoDesu/UITableViewDiffableDataSource-Swift/blob/main/Gif/RocketSim%20Recording%20-%20iPhone%2012%20-%202021-08-02%2012.25.37.gif" width="300"></td> </tr> </table> override func viewDidLoad() {
super.viewDidLoad()
let moveScaleTransform = getScaleAnimation()
// Make the button invisible
for rateButton in rateButtons {
rateButton.transform = moveScaleTransform
rateButton.alpha = 0
}
}
func getScaleAnimation() -> CGAffineTransform {
let moveRightTransform = CGAffineTransform.init(translationX: 600, y: 0)
let scaleUpTransform = CGAffineTransform.init(scaleX: 5.0, y: 5.0)
return scaleUpTransform.concatenating(moveRightTransform)
}
func setSpringAnimation() {
var delay: TimeInterval = 0.1
for rateButton in self.rateButtons {
UIView.animate(withDuration: 0.8, delay: delay, usingSpringWithDamping: 0.2,
initialSpringVelocity: 0.3, options: [], animations
:{
rateButton.alpha = 1.0
rateButton.transform = .identity
}, completion: nil)
delay += 0.1
}
UIView.animate(withDuration: 0.4, delay: 0.1, options: [], animations:
{
self.closeButton.alpha = 1.0
}, completion: nil)
}
func setStandardAnimation() {
var delay: TimeInterval = 0.1
for rateButton in self.rateButtons {
UIView.animate(withDuration: 0.4, delay: delay, options: [], animations:
{
rateButton.alpha = 1.0
rateButton.transform = .identity
}, completion: nil)
delay += 0.1
}
UIView.animate(withDuration: 0.4, delay: 0.1, options: [], animations:
{
self.closeButton.alpha = 1.0
}, completion: nil)
}
Importing photos with UIImagePickerController
<img src="https://github.com/YamamotoDesu/UITableViewDiffableDataSource-Swift/blob/main/Gif/RocketSim%20Recording%20-%20iPhone%2012%20-%202021-08-03%2009.46.44.gif" width="300">https://github.com/YamamotoDesu/UITableViewDiffableDataSource-Swift/blob/main/FoodPin/Controller/NewRestaurantController.swift
extension NewRestaurantController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
photoImageView.image = selectedImage
photoImageView.contentMode = .scaleAspectFit
photoImageView.clipsToBounds = true
}
dismiss(animated: true, completion: nil)
}
}
Returns the swipe actions to display on the leading edge of the row(iOS 11 and onward)
<img src="https://user-images.githubusercontent.com/47273077/127734414-c5409c84-54a4-41e7-9ff1-f140e2185d8b.png" width="200">
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
// Get the selected restaurant
guard let restaurant = self.dataSource.itemIdentifier(for: indexPath) else {
return UISwipeActionsConfiguration()
}
// Favorite action
let favoriteAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
let cell = tableView.cellForRow(at: indexPath) as! RestaurantTableViewCell
cell.heartMark.isHidden = !restaurant.isFavorite
cell.heartMark.tintColor = UITraitCollection.isDarkMode ? .systemYellow : .blue
self.restaurantIsFavorites[indexPath.row] = !restaurant.isFavorite
tableView.rel
Related Skills
node-connect
341.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
341.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.5kCommit, push, and open a PR
