Обновление данных после загрузки изображения
Всем привет, есть таблица, в ее ячейки нужно загрузить изображение, в searchBar записываем URL, потом по тапу на кнопку должна происходить загружка в ячейку. как обновить данные в таблице в моем случае
ImageLoaderCell
import UIKit
import Alamofire
import AlamofireImage
final class ImageLoaderCell: UITableViewCell {
// MARK: - View
private let mainImageView = UIImageView()
private var ulr: URL?
// MARK: - Property
static let identifaer = String(describing: ImageLoaderCell.self)
private enum Constants {
static let verticalSpace: CGFloat = 10;
static let horizontalScape: CGFloat = 18;
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViewAppearance()
setupViewLayout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(url: URL?) {
self.ulr = url
//loadImage()
}
func loadImage() {
guard let url = ulr else {return}
AF.request(url.absoluteString).responseImage { [weak self] response in
if case .success(let image) = response.result {
self?.mainImageView.image = image
//print("Картинка загружена: \(image)")
}
}
}
}
// MARK: - Appearance
private extension ImageLoaderCell {
func setupViewAppearance() {
setupMainImageViewAppearance()
}
func setupMainImageViewAppearance() {
mainImageView.contentMode = .scaleAspectFit
}
}
// MARK: - Layout
private extension ImageLoaderCell {
func setupViewLayout() {
setupMainImageViewLayout()
}
func setupMainImageViewLayout() {
contentView.addSubview(mainImageView)
mainImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
mainImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: Constants.verticalSpace),
mainImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -Constants.verticalSpace),
mainImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,
constant: Constants.horizontalScape),
mainImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,
constant: -Constants.horizontalScape),
])
}
}
ImageLoaderView
import UIKit
final class ImageLoaderView: UIView {
// MARK: - View
private let searchBar = UISearchBar()
private let tableView = UITableView()
private let searchButton = UIButton()
private var tableViewDataSource: TableViewDataSource?
private var tableViewDelegate: TableViewDelegate?
private var image = [URL]()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .systemBackground
tableView.register(ImageLoaderCell.self, forCellReuseIdentifier: ImageLoaderCell.identifaer)
setupViewAppearance()
setupViewLayout()
searchButton.addTarget(self, action: #selector(press), for: .touchUpInside)
self.tableViewDelegate = TableViewDelegate()
self.tableViewDataSource = TableViewDataSource(withData: image)
self.tableView.delegate = self.tableViewDelegate
self.tableView.dataSource = self.tableViewDataSource
}
@objc func press() {
if let searchBarText = searchBar.text {
if let urlImage = URL(string: searchBarText) {
image.append(urlImage)
tableView.reloadData()
}
else {
print("Пусто")
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - Appearance
private extension ImageLoaderView {
func setupViewAppearance() {
setupSearchBarAppearance()
setupTableViewAppearance()
setupSearchButtonApperance()
}
func setupSearchBarAppearance() {
searchBar.placeholder = "Введите URL"
}
func setupTableViewAppearance() {
tableView.tableFooterView = UIView()
}
func setupSearchButtonApperance() {
searchButton.setTitle("load", for: .normal)
searchButton.setTitleColor(.blue, for: .normal)
}
}
// MARK: - Layouts
private extension ImageLoaderView {
func setupViewLayout() {
setupSearchBarLayout()
setupTableViewLayout()
setupSearchButtonLayout()
}
func setupSearchBarLayout() {
self.addSubview(searchBar)
searchBar.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
searchBar.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
searchBar.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
searchBar.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -50),
])
}
func setupTableViewLayout() {
self.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: searchBar.bottomAnchor),
tableView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor),
])
}
func setupSearchButtonLayout() {
self.addSubview(searchButton)
searchButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
searchButton.widthAnchor.constraint(equalToConstant: 50),
searchButton.heightAnchor.constraint(equalToConstant: 50),
searchButton.centerYAnchor.constraint(equalTo: searchBar.centerYAnchor),
searchButton.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -10)
])
}
}
ImageLoaderViewController
import UIKit
class ImageLoaderViewController: UIViewController {
private let imageLoaderView = ImageLoaderView()
override func loadView() {
super.loadView()
self.view = imageLoaderView
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
ImageLoaderDataSource
import UIKit
class TableViewDataSource: NSObject, UITableViewDataSource{
private var image = [URL]()
init(withData image: [URL]) {
self.image = image
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.image.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: ImageLoaderCell.identifaer, for: indexPath) as? ImageLoaderCell
else { return UITableViewCell() }
cell.configure(url: image[indexPath.row])
cell.loadImage()
return cell
}
}
ImageLoaderDelegete
import UIKit
final class TableViewDelegate: NSObject, UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}