ios - 显示自定义标签和图像重复的 UITableViewCell

标签 ios swift uitableview

New image added 图像仍然显示顶部和底部的单元格

Screen shot

我正在使用 indexPath.row 来确定要显示的图像和标签的类型,但不幸的是它也显示在表格的顶部和底部,因此复制下面的单元格是我的代码:

导入基金会 导入 UIKit

//custom cell

类 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: 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")
}

最佳答案

不要在运行时添加 UIImageUILable 尝试使用自定义单元格。有关如何使用自定义和默认单元格的更多详细信息,请遵循本教程 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
    }

关于ios - 显示自定义标签和图像重复的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53811659/

相关文章:

ios - 通话后恢复应用程序

ios - 如何将阴影添加到 UIButton 边框而不是文本

ios - 如何重新排列 UILocalNotification tableview 的单元格?

ios - UITableView 中的第一部分从来​​没有正确的内容

ios - UITableView的可重用池如何查看内容

ios - 如何在 swift3 中获取 CNLabeledValue 的本地化字符串

ios - 针对用户类 Parse ios 中的特定用户更新记录

ios - 推送一个重复的 View Controller

ios - Swift WKWebView 未初始化并导致意外的 nil 错误

iphone - 如何使用 corebluetooth 检测最近的 BLE 4.0 ios 设备?