我正在使用indexPath.row来确定要显示的图像和标签的类型,但不幸的是,它也显示在表格的顶部和底部,因此下面是我的代码:
导入基金会 导入UIKit
//custom cell
menuTableTableCells类: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")
}
}
答案 0 :(得分:1)
与其在运行时添加UIImage
和UILable
,请尝试使用自定义单元格。有关如何使用自定义和默认单元格的更多详细信息,请遵循本教程Here
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
let headline = headlines[indexPath.row]
cell.textLabel?.text = headline.title
cell.imageView?.image = UIImage(named: headline.image)
return cell
}
答案 1 :(得分:0)
//My custom class table cells
import Foundation
导入UIKit
menuTableTableCells类: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: self.menuImage.trailingAnchor, constant: 20).isActive = true
lblMenuName.topAnchor.constraint(equalTo: self.topAnchor, constant: 25).isActive = true
// lblMenuName.trailingAnchor.constraint(equalTo:self.trailingAnchor,常数:-10).isActive = true lblMenuName.font = UIFont.systemFont(ofSize:18) lblMenuName.textColor = .white
}
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 variables
struct MenuCellData {
let menuName: String?
let menuIcon: UIImage?
}
var populateData = [MenuCellData]()
//populated my array with menu items
populateData = [MenuCellData.init(menuName: "Home", menuIcon: #imageLiteral(resourceName: "home-1")), MenuCellData.init(menuName: "The Prophet", menuIcon: #imageLiteral(resourceName: "devotion")), MenuCellData.init(menuName: "Devotions", menuIcon: #imageLiteral(resourceName: "devotion")), MenuCellData.init(menuName: "Church Events", menuIcon: #imageLiteral(resourceName: "events")), MenuCellData.init(menuName: "Ahofadiekrom", menuIcon: #imageLiteral(resourceName: "worship")), MenuCellData.init(menuName: "Branches", menuIcon: #imageLiteral(resourceName: "branches")), MenuCellData.init(menuName: "Gallery", menuIcon: #imageLiteral(resourceName: "gallery")), MenuCellData.init(menuName: "Videos", menuIcon: #imageLiteral(resourceName: "videos")), MenuCellData.init(menuName: "Live Streaming", menuIcon: #imageLiteral(resourceName: "live")), MenuCellData.init(menuName: "Live Radio", menuIcon: #imageLiteral(resourceName: "radio")), MenuCellData.init(menuName: "Elijah TV", menuIcon: #imageLiteral(resourceName: "logo1")), MenuCellData.init(menuName: "Contact Us", menuIcon: #imageLiteral(resourceName: "contact"))]
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") as! menuTableCells
cell.backgroundColor = UIColor.clear
cell.selectionStyle = .none
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
}
中提琴!!!