ios - 使自定义 UITableView 单元格正确显示内容(非空白)

标签 ios swift uitableview

我一直在致力于制作一个 iOS 应用程序,它需要一个可滚动的屏幕/ View ,并且有一个图像,然后是一个列表,然后是一个图像,然后是另一个图像(附件是我制作的 Android 版本的屏幕截图)

Top of the view

View Scrolled

我尝试使用以下代码,它为我提供了正确数量的单元格,但它们都是空白的。

//
//  ServicesTableViewController.swift
//  Contact Australis
//
//  Created by Raghav Khanna on 22/4/18.
//  Copyright © 2018 Australis. All rights reserved.
//

import UIKit
class ServiceViewCell: UITableViewCell {
@IBOutlet weak var IMage: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    IMage.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}
class ServiceViewCellList: UITableViewCell {

@IBOutlet weak var somethin_label: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    let color = UIColor(red: 0/255, green: 105/255, blue: 191/255, alpha: 1.0).cgColor
    let back_colour = UIColor(red: 212/255, green: 242/255, blue: 253/255, alpha: 1.0).cgColor
    let back_colour_ui = UIColor(red: 212/255, green: 242/255, blue: 253/255, alpha: 1.0)
    let radius: CGFloat = 5
    let border_width:CGFloat = 1.5
    somethin_label.layer.borderColor = color
    somethin_label.layer.borderWidth = border_width
    somethin_label.layer.cornerRadius = radius
    somethin_label.backgroundColor = back_colour_ui
}
var items_maintenance = ["Painting","All Lighting & Globe Replacemt", 
"Carpet & Hard Floor Replacement","Electrical Work & Maintenance","Plumbing Work & Maintenance","Test & Tag Completion","Office Furniture Removal", "Hard Waste Removal", "Window Frosting", "All Other Handy Man & Maintenance Tasks"]
override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
} class ServiceViewCellCleaning: UITableViewCell {

@IBOutlet weak var Title: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
   Title.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}
class ServiceViewCellCleaningList: UITableViewCell {
@IBOutlet weak var other_label: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    let color = UIColor(red: 0/255, green: 105/255, blue: 191/255, alpha: 1.0).cgColor
    let back_colour = UIColor(red: 212/255, green: 242/255, blue: 253/255, alpha: 1.0).cgColor
    let back_colour_ui = UIColor(red: 212/255, green: 242/255, blue: 253/255, alpha: 1.0)
    let radius: CGFloat = 5
    let border_width:CGFloat = 1.5
    other_label.layer.borderColor = color
    other_label.layer.borderWidth = border_width
    other_label.layer.cornerRadius = radius
    other_label.backgroundColor = back_colour_ui
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}
class ServicesTableViewController: UITableViewController {
let basicCellIdentifier = "BasicCell"
var items_maintenance = ["Painting","All Lighting & Globe Replacement", "Carpet & Hard Floor Replacement","Electrical Work & Maintenance","Plumbing Work & Maintenance","Test & Tag Completion","Office Furniture Removal", "Hard Waste Removal", "Window Frosting", "All Other Handy Man & Maintenance Tasks"]
var items_cleaning = ["All Genral Comercial Cleaning","Office Cleaning", "Initial Clean","Spring Clean","Steam Carpet Cleaning","Window Washing","High Pressure Washing", "Waste Removal", "Strip & Seal Hard Floors", "Scrubbing & Buffing Hard Floors"]
let cellSpacingHeight: CGFloat = 5
@IBOutlet var table: UITableView!
func configureTableView() {


    //tableView.rowHeight = UITableViewAutomaticDimension
    //tableView.estimatedRowHeight = 1000.0
    //let rect = CGRect(origin: .zero, size: CGSize(width: 400, height: 400))
    //self.tableView = UITableView(frame: rect, style: UITableViewStyle.plain)
    table.register(ServiceViewCell.self, forCellReuseIdentifier: "maintenance")
     table.register(ServiceViewCellList.self, forCellReuseIdentifier: "customcell")
    table.register(ServiceViewCellCleaning.self, forCellReuseIdentifier: "cleaning")
    table.register(ServiceViewCellCleaningList.self, forCellReuseIdentifier: "cleaning_customcell")

}



/*func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return
}*/




override func viewDidLoad() {
    super.viewDidLoad()
    self.configureTableView()
    table.reloadData()
    table.delegate = self
    table.dataSource = self
    // Uncomment the following line to preserve selection between presentations
    self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 4
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 1
    } else if section == 1 {
      return items_maintenance.count
    } else if section == 2 {
        return 1
    }
    else {
        return items_cleaning.count
    }

}



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let maintenance_title = table.dequeueReusableCell(withIdentifier: "maintenance", for: indexPath) as! ServiceViewCell
    let maintenance_list = table.dequeueReusableCell(withIdentifier: "customcell", for: indexPath) as! ServiceViewCellList
    let cleaning_title = table.dequeueReusableCell(withIdentifier: "cleaning", for: indexPath) as! ServiceViewCellCleaning
    let cleaning_list = table.dequeueReusableCell(withIdentifier: "cleaning_customcell", for: indexPath) as! ServiceViewCellCleaningList

    maintenance_list.somethin_label?.text = self.items_maintenance[indexPath.row]

    maintenance_list.somethin_label?.adjustsFontSizeToFitWidth = false

    maintenance_list.somethin_label?.font = UIFont.systemFont(ofSize: 10.0)

    cleaning_list.other_label?.text = "test"
    cleaning_list.other_label?.adjustsFontSizeToFitWidth = false
    cleaning_list.other_label?.font = UIFont.systemFont(ofSize: 10.0)

    cleaning_title.Title?.image = UIImage(named: "cleaning.png")
    maintenance_title.IMage?.image = UIImage(named: "maintenance.png")
    if indexPath.section  == 0 {
    return maintenance_title
    } else if indexPath.section == 1 {
        return maintenance_list
    } else if indexPath.section == 2 {
        return cleaning_title
    }
else {
        return cleaning_list
    }


    return cleaning_list

}


/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Delete the row from the data source
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

}
*/


//Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return false
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

My Storyboard looks like this (它是主 Storyboard中带有自定义类的原型(prototype)单元的另一个 View ),我正在努力弄清楚为什么我总是得到“maintenance_list.somethin_label!.text = self.items_maintenance”的“在展开可选值时意外发现nil” [indexPath.row]"或 this (blank cells) 当我使用“?”时代替 '!'。

我知道为什么在使用“?”时解包时没有得到 nil 错误。但真正的问题是为什么我无法与每个单元格中的 View 进行交互以显示所需的数据。我检查了所有的网点,都是正确的。

任何帮助将不胜感激。

提前致谢。

最佳答案

如果无法访问整个项目,很难说它为什么不起作用。

但我认为您遵循的方法不正确,您应该考虑只有两个部分(维护、清洁),然后维护和清洁的每个项目都是一个单元格,因此您的数据源应该返回 2 个部分和 10每个部分的行。

您需要一个节标题,其中包含一个 ImageView ,然后只有一个原型(prototype)单元格,您可以将其重复用于任何行。

希望这有帮助。

关于ios - 使自定义 UITableView 单元格正确显示内容(非空白),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50106100/

相关文章:

ios - 如何更改 "share your information"中的公司名称以在 iTunes 中自动续订?

ios - 呈现 UICollectionView 时防止 UICollectionViewCell 动画外观

iphone - 如何调整 UISwitch 按钮的大小?

ios - 检测首次启动时出错(Swift)

ios - 自定义 UITableviewCell 放弃内存问题

ios - Swift 3.0 中 UIView 内的 UITableView

ios - ASIHttpRequest 仅支持 IPv6 吗?

ios - 在改变它们的同时传递 JSON 对象

ios - SnapKit 和动态 UITableViewCell 布局不正确

objective-c - 滚动时表格单元格内容损坏