6.1. Updating the TableView Cell to accommodate an ImageView
//
// TableViewExpenseCell.swift
// App6
//
// Created by Sakib Miazi on 5/18/23.
//
import UIKit
class TableViewExpenseCell: UITableViewCell {
//codes omitted...
//MARK: declaring the ImageView for receipt image...
var imageReceipt: UIImageView!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
//Codes omitted...
//MARK: defining the ImageView for receipt image...
setupimageReceipt()
initConstraints()
}
//Codes omitted...
//Adding the ImageView for receipt...
func setupimageReceipt(){
imageReceipt = UIImageView()
imageReceipt.image = UIImage(systemName: "photo")
imageReceipt.contentMode = .scaleToFill
imageReceipt.clipsToBounds = true
imageReceipt.layer.cornerRadius = 10
imageReceipt.translatesAutoresizingMaskIntoConstraints = false
wrapperCellView.addSubview(imageReceipt)
}
//MARK: initializing the constraints...
func initConstraints(){
NSLayoutConstraint.activate([
wrapperCellView.topAnchor.constraint(equalTo: self.topAnchor,constant: 10),
wrapperCellView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10),
wrapperCellView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10),
wrapperCellView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -10),
labelTitle.topAnchor.constraint(equalTo: wrapperCellView.topAnchor, constant: 2),
labelTitle.leadingAnchor.constraint(equalTo: imageReceipt.trailingAnchor, constant: 8),
labelTitle.heightAnchor.constraint(equalToConstant: 32),
labelTitle.widthAnchor.constraint(lessThanOrEqualTo: wrapperCellView.widthAnchor),
labelAmount.topAnchor.constraint(equalTo: labelTitle.bottomAnchor, constant: 2),
labelAmount.leadingAnchor.constraint(equalTo: labelTitle.leadingAnchor),
labelAmount.heightAnchor.constraint(equalToConstant: 32),
labelAmount.widthAnchor.constraint(lessThanOrEqualTo: labelTitle.widthAnchor),
labelType.topAnchor.constraint(equalTo: labelAmount.bottomAnchor, constant: 2),
labelType.leadingAnchor.constraint(equalTo: labelTitle.leadingAnchor),
labelType.heightAnchor.constraint(equalToConstant: 32),
labelType.widthAnchor.constraint(lessThanOrEqualTo: labelTitle.widthAnchor),
imageReceipt.leadingAnchor.constraint(equalTo: wrapperCellView.leadingAnchor, constant: 8),
imageReceipt.centerYAnchor.constraint(equalTo: wrapperCellView.centerYAnchor),
//MARK: it is better to set the height and width of an ImageView with constraints...
imageReceipt.heightAnchor.constraint(equalTo: wrapperCellView.heightAnchor, constant: -20),
imageReceipt.widthAnchor.constraint(equalTo: wrapperCellView.heightAnchor, constant: -20),
wrapperCellView.heightAnchor.constraint(equalToConstant: 104)
])
}
//codes omitted...
}
Appendix
Installing and using SF Symbols app


Previous6. UIMenu, Picking Images from Gallery and Camera, and UIImageViewNext6.2. Add Expense Screen
Last updated

