Skip to content

Instantly share code, notes, and snippets.

@mats-claassen
Last active September 29, 2021 13:39
Show Gist options
  • Save mats-claassen/7add73434216ee9cd70309c7e8deba31 to your computer and use it in GitHub Desktop.
Save mats-claassen/7add73434216ee9cd70309c7e8deba31 to your computer and use it in GitHub Desktop.
Example of an Eureka custom row
//
// UserInfoCell.swift
//
// Created by Mathias Claassen on 30/8/16.
// Copyright © 2016 Xmartlabs. All rights reserved.
//
import Foundation
import Eureka
final class UserInfoRow: Row<UserInfoCell>, RowType {
required init(tag: String?) {
super.init(tag: tag)
cellProvider = CellProvider<UserInfoCell>(nibName: "UserInfoCell")
}
}
final class UserInfoCell: Cell<User>, CellType {
@IBOutlet weak var userImageView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var emailLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
private static var dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
return dateFormatter
}()
required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func setup() {
super.setup()
// we do not want our cell to be selected in this case. If you use such a cell in a list then you might want to change this.
selectionStyle = .none
// configure our profile picture imageView
userImageView.contentMode = .scaleAspectFill
userImageView.clipsToBounds = true
// define fonts for our labels
nameLabel.font = .systemFont(ofSize: 18)
emailLabel.font = .systemFont(ofSize: 13.3)
dateLabel.font = .systemFont(ofSize: 13.3)
// set the textColor for our labels
for label in [emailLabel, dateLabel, nameLabel] {
label?.textColor = .gray
}
// specify the desired height for our cell
height = { return 94 }
// set a light background color for our cell
backgroundColor = UIColor(red:0.984, green:0.988, blue:0.976, alpha:1.00)
}
override func update() {
super.update()
// we do not want to show the default UITableViewCell's textLabel
textLabel?.text = nil
// get the value from our row
guard let user = row.value else { return }
// set the image to the userImageView. You might want to do this with AlamofireImage or another similar framework in a real project
if let url = user.pictureUrl, let data = try? Data(contentsOf: url) {
userImageView.image = UIImage(data: data)
} else {
userImageView.image = UIImage(named: "placeholder")
}
// set the texts to the labels
emailLabel.text = user.email
nameLabel.text = user.name
dateLabel.text = UserInfoCell.dateFormatter.string(from: user.dateOfBirth)
}
}
struct User: Equatable {
var name: String
var email: String
var dateOfBirth: Date
var pictureUrl: URL?
}
func ==(lhs: User, rhs: User) -> Bool {
return lhs.email == rhs.email
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" rowHeight="106" id="Fna-AV-aAc" customClass="UserInfoCell" customModule="Example" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="414" height="93"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Fna-AV-aAc" id="w2d-sH-YQT">
<rect key="frame" x="0.0" y="0.0" width="414" height="92"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="QZB-wy-08L">
<rect key="frame" x="25" y="8" width="76" height="76"/>
<constraints>
<constraint firstAttribute="width" secondItem="QZB-wy-08L" secondAttribute="height" id="e0t-j5-Ef3"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="zET-oM-5uq">
<rect key="frame" x="119" y="8" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qJk-3r-z7X">
<rect key="frame" x="119" y="42" width="36" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="E5k-2r-Xfz">
<rect key="frame" x="119" y="67" width="36" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="qJk-3r-z7X" firstAttribute="centerY" secondItem="w2d-sH-YQT" secondAttribute="centerY" constant="4" id="0Fi-3N-6OI"/>
<constraint firstItem="QZB-wy-08L" firstAttribute="top" secondItem="w2d-sH-YQT" secondAttribute="top" constant="8" id="7pp-9p-Snl"/>
<constraint firstItem="zET-oM-5uq" firstAttribute="leading" secondItem="qJk-3r-z7X" secondAttribute="leading" id="Ec3-UZ-cxX"/>
<constraint firstItem="zET-oM-5uq" firstAttribute="top" secondItem="QZB-wy-08L" secondAttribute="top" id="Gbc-ke-8eu"/>
<constraint firstItem="QZB-wy-08L" firstAttribute="leading" secondItem="w2d-sH-YQT" secondAttribute="leading" constant="25" id="LsV-Qr-IaU"/>
<constraint firstItem="zET-oM-5uq" firstAttribute="leading" secondItem="QZB-wy-08L" secondAttribute="trailing" constant="18" id="Rlk-Rz-7iX"/>
<constraint firstItem="E5k-2r-Xfz" firstAttribute="bottom" secondItem="QZB-wy-08L" secondAttribute="bottom" id="ll6-dt-qxT"/>
<constraint firstItem="qJk-3r-z7X" firstAttribute="leading" secondItem="E5k-2r-Xfz" secondAttribute="leading" id="njk-8s-iaH"/>
<constraint firstAttribute="bottom" secondItem="QZB-wy-08L" secondAttribute="bottom" constant="8" id="tDG-xb-Qyx"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="dateLabel" destination="E5k-2r-Xfz" id="VWE-Ow-KQR"/>
<outlet property="emailLabel" destination="qJk-3r-z7X" id="1p7-r7-r7f"/>
<outlet property="nameLabel" destination="zET-oM-5uq" id="bc1-SL-nDC"/>
<outlet property="userImageView" destination="QZB-wy-08L" id="uMO-Tc-XIy"/>
</connections>
<point key="canvasLocation" x="279" y="232.5"/>
</tableViewCell>
</objects>
</document>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment