UITableViewCell отображает пользовательские метки и дубликаты изображений - PullRequest
0 голосов
/ 17 декабря 2018

New image added Изображение по-прежнему показывает ячейки сверху и снизу

Screen shot

Я использую 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")
}

}

Ответы [ 2 ]

0 голосов
/ 19 декабря 2018
//My custom class table cells
import Foundation

import UIKit

class menuTableCells: 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, constant: -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
}

Виола!!!

0 голосов
/ 17 декабря 2018

Вместо добавления UIImage и UILable во время выполнения попробуйте использовать пользовательскую ячейку.Для получения более подробной информации о том, как использовать настраиваемую ячейку и ячейку по умолчанию, следуйте этому руководству Здесь

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
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...