Изображение по-прежнему показывает ячейки сверху и снизу
Я использую indexPath.row для определениятип изображения и метки для отображения, но, к сожалению, они отображаются вверху, а также в самом низу таблицы, поэтому дублирующиеся ячейки ниже - мой код:
import Foundation import UIKit
//custom cell
classmenuTableCells: UITableViewCell {
var menuName: String?
var menuIcon: UIImage?
var lblMenuName: UILabel = {
let view = UILabel()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var menuImage: UIImageView = {
let view = UIImageView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(lblMenuName)
self.addSubview(menuImage)
//constraints for icons
menuImage.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
menuImage.topAnchor.constraint(equalTo: self.topAnchor, constant: 20).isActive = true
menuImage.heightAnchor.constraint(equalToConstant: 35).isActive = true
menuImage.widthAnchor.constraint(equalToConstant: 35).isActive = true
//constraints for label menu
lblMenuName.leadingAnchor.constraint(equalTo: menuImage.trailingAnchor, constant: 20).isActive = true
lblMenuName.topAnchor.constraint(equalTo: self.topAnchor, constant: 20).isActive = true
lblMenuName.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -10).isActive = true
}
override func layoutSubviews() {
super.layoutSubviews()
if let menuName = menuName{
lblMenuName.text = menuName
}
if let menuIcon = menuIcon {
menuImage.image = menuIcon
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//struct to hold data
struct MenuCellData {
let menuName: String?
let menuIcon: UIImage?
}
class SidebarView: UIView, UITableViewDelegate, UITableViewDataSource{
var titleArr = [String]()
var iconsArr = [String]()
var populateData = [MenuCellData]()
weak var delegate: SidebarViewDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.black.withAlphaComponent(0.80)
self.clipsToBounds = true
titleArr = ["Home", "The Prophet", "Devotions", "Church Events", "Ahofadiekrom", "Branches", "Gallery", "Videos", "Live Streaming", "Live Radio", "Elijah TV", "Contact Us"]
iconsArr = ["home-1", "devotion", "devotion", "events", "worship", "branches", "gallery", "videos", "live", "radio", "logo1", "contact"]
setUpViews()
myTableView.delegate = self
myTableView.dataSource = self
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
myTableView.tableFooterView = UIView()
myTableView.separatorStyle = UITableViewCellSeparatorStyle.none
myTableView.allowsSelection = true
myTableView.bounces = false
myTableView.showsVerticalScrollIndicator = false
myTableView.backgroundColor = UIColor.clear
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return populateData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! menuTableCells
cell.backgroundColor = UIColor.clear
cell.selectionStyle = .none
let menus = MenuCellData(menuName: titleArr[indexPath.row], menuIcon: UIImage(named: iconsArr[indexPath.row]))
populateData.append(menus)
myTableView.reloadData()
cell.menuName = populateData[indexPath.row].menuName
cell.menuIcon = populateData[indexPath.row].menuIcon
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate?.sidebarDidSelectRow(row: Row(row: indexPath.row))
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
let appLogo: UIImageView = {
let view = UIImageView(image: #imageLiteral(resourceName: "logo"))
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let myTableView: UITableView = {
let table = UITableView()
table.translatesAutoresizingMaskIntoConstraints = false
return table
}()
func setUpViews(){
self.addSubview(appLogo)
appLogo.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
appLogo.topAnchor.constraint(equalTo: topAnchor).isActive = true
appLogo.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
appLogo.heightAnchor.constraint(equalToConstant: 200).isActive = true
self.addSubview(myTableView)
myTableView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
myTableView.topAnchor.constraint(equalTo: appLogo.bottomAnchor).isActive = true
myTableView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}