Ios 使用XPath生成具有结构和数据的tableView单元格
我已经知道如何制作一个基本的tableView,下面是我的基本代码Ios 使用XPath生成具有结构和数据的tableView单元格,ios,swift,uitableview,dictionary,indexpath,Ios,Swift,Uitableview,Dictionary,Indexpath,我已经知道如何制作一个基本的tableView,下面是我的基本代码 class ShoppingTableViewController: UITableViewController{ var description = ["D1", "299900", "D2", "P201712310000003000", "D3", "ASS+DfDFxSuu", "D10&qu
class ShoppingTableViewController: UITableViewController{
var description = ["D1", "299900", "D2", "P201712310000003000", "D3", "ASS+DfDFxSuu", "D10", "901", "D11", "00,46246226301561000110001001", "D12", "20201231123030"]
var dictDescription = ["D10": "901", "D11": "00,46246226301561000110001001", "D3": "ASS+DfDFxSuu", "D12": "20201231123030", "D2": "P201712310000003000", "D1": "299900"]
override func viewDidLoad() {
super.viewDidLoad()
// Xib
tableView.register(UINib(nibName:PropertyKeys.pictureCell , bundle: nil), forCellReuseIdentifier: PropertyKeys.pictureCell)
tableView.register(UINib(nibName:PropertyKeys.infoCell , bundle: nil), forCellReuseIdentifier: PropertyKeys.infoCell)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.pictureCell, for: indexPath) as! PictureWithTableViewCell
cell.iconImageView.image = UIImage(named: "amount")
cell.lbDescription.text = "Type"
cell.lbName.text = "Shopping"
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.infoCell, for: indexPath) as! InfoTableViewCell
cell.lbDescription.text = "Taiwan dollar"
cell.lbName.text = m_dictDescription["D1"]
return cell
case 2:
let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.pictureCell, for: indexPath) as! PictureWithTableViewCell
cell.iconImageView.image = UIImage(named: "info")
cell.lbDescription.text = "BankName"
cell.lbName.text = m_dictDescription["D11"]
return cell
case 3:
if m_dictDescription["D2"] != nil {
let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.infoCell, for: indexPath) as! InfoTableViewCell
cell.lbDescription.text = "orderNumber"
cell.lbName.text = m_dictDescription["D2"]
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.infoCell, for: indexPath) as! InfoTableViewCell
cell.isHidden = true
return cell
}
但这是一种不安全的方法,因为我将func行数和行的单元格作为硬代码编写。
所以我想改变tableView的组成方式,先决定单元格格式,然后填充数据(就像我的基本代码一样),让数据决定我的行数和每行的单元格数。(使用indexath)
但我遇到了一些问题:我试着写:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let test = self.m_arrDescription[indexPath.row]
cell.lbName.text = test
它工作,但每个细胞看起来一样,而我想提出不同的细胞。
我在互联网上搜索一些信息,也许我可以使用struct并结合m_arrDescription来制作tableview单元格
// use struct to decide cell label or image , for example: cell.lbDescription.text ...
struct CellFormat {
var title : String
var image : UIImage
var name : String
}
到目前为止,这是我写的,谁能帮我继续写下去?
很清楚,在这段代码中如何使用[indexPath.row]?请创建第一个枚举
enum CellType: String, CaseIterable {
case title, image, name
}
然后在tableview委托方法中使用此代码
switch CellType(rawValue: indexPath.row) {
case .title:
break
case .image:
break
case .name:
break
case .none:
break
}
如果有任何问题,请让我知道hi,我会尝试在func单元格中为tableview的{}行编写:switch CellType(rawValue:indexPath.row){},但Xcode警告我一个错误:“无法将Int类型的值转换为预期的参数类型字符串”,这非常令人困惑。编辑你的问题,试着用通俗易懂的语言解释你想做什么。@DonMag很抱歉我的解释不好,我重新编辑了问题,谢谢。这仍然让人困惑。。。您的帖子包括
ShoppingTableViewController
。。。您是否试图显示“购物项目”的列表(表视图),然后在选择某一行时显示该项目的详细信息?