如何在tableview中列出数据

Posted

技术标签:

【中文标题】如何在tableview中列出数据【英文标题】:How to list the data in the tableview 【发布时间】:2018-11-16 04:26:24 【问题描述】:

这是屏幕截图。根据this:

我已经有了如下代码。

func numberOfSections(in tableView: UITableView) -> Int 
      return questionViewModel.numberOfSections()
     
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat 
        return 100
      func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
        let identifier = "HeaderCell"
       var headercell: questionheader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? questionheader
        if headercell == nil 
            tableView.register(UINib(nibName: "questionheader", bundle: nil), forCellReuseIdentifier: identifier)
            headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_questionheader
             headercell.setReviewData(reviews:questionViewModel.titleForHeaderInSection(atsection:section))
            headercell.isUserInteractionEnabled = false
        return headercell

    
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
         return questionViewModel.numberOfRowsIn(section: section)
     

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
        let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
        print(model.answerType)
       print(model.answerType?.rawValue)
        let c = model.answerType
        return c!.cellType().getHeight()
    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
         let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
            print(model.answerType)
            print(model.answerType?.rawValue)
            let c = model.answerType
            let cellClass = c?.cellType().getClass()
            print(cellClass)
            let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
            print(cell)
         cell.selectionStyle = .none
 let optionModel = questionViewModel.datafordisplay(atindex: indexPath)
          cell.setOptions(Options1: optionModel)
         cell.delegate = self
        if optionModel.isSelected!
                    
                    print(optionModel.isSelected)
                    cell.setOptions1(OptionsSelected:optionModel)
        
         else 
                    print(optionModel.isSelected)
                    cell.setOptions1(OptionsisSelected:optionModel)
         
        cell.type = c?.cellType()
         print(cell.type)
       else if cell.type == .radiotype
            cell.selectionStyle = .none
        
       return cell
   

这是我的代码。但据此,我将得到如下屏幕截图的输出。

实际上,最初我需要将部分标题显示为: - 请介绍一下我们

之后是小节。小节问题标题如下:-1。知识2.友好与否

然后在小节中显示选项。如何执行?

【问题讨论】:

这个怎么实现? 到目前为止您为实现这一目标所做的工作 @AbdulRehmanWarraich 我已经添加了我的代码,请检查。如何实现? 请重新格式化代码并删除许多不必要的空行。 @vadian 我已经编辑了。如何实现? 【参考方案1】:

您可以在表格上方显示“请告诉我们”为UILabel

【讨论】:

如何使用嵌套部分? 您不需要嵌套部分。只是一个带有文本的标签和带有选项的表格。【参考方案2】:

下面的例子可以帮助你。

  let sections = [ // All sections
        [ // Sports section
            ["Baseball", "Softball", "Cricket"], // Bat-and-Ball sub-section
            ["Field Hockey", "Ice Hockey", "Roller Hockey"] // Hockey subsection
        ], [ // Engineering section
            ["Software Engineer", "Electrical Engineer"] // Computer Science subsection
        ]
    ]


    func numberOfSections(in tableView: UITableView) -> Int 
        return sections.count
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        let sectionItems = sections[section]
        var numberOfRows: Int = sectionItems.count // For second level section headers
        for rowItems: [Any] in sectionItems as? [[Any]] ?? [] 
            numberOfRows += rowItems.count // For actual table rows
        
        return numberOfRows
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        var sectionItems = sections[indexPath.section]
        var sectionHeaders = self.sectionHeaders[indexPath.section]
        let itemAndSubsectionIndex: IndexPath? = computeItemAndSubsectionIndex(for: indexPath)
        let subsectionIndex = Int(itemAndSubsectionIndex?.section ?? 0)
        let itemIndex: Int? = itemAndSubsectionIndex?.row

        if (itemIndex ?? 0) < 0 
            // Section header
            let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "SECTION_HEADER_CELL", for: indexPath)
            cell.textLabel?.text = sectionHeaders[subsectionIndex] as? String
            return cell
        
        else
            let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "ROW_CONTENT_CELL", for: indexPath)
            cell.textLabel?.text = sectionItems[subsectionIndex][itemIndex ?? 0] as? String
            return cell
        
    
    func computeItemAndSubsectionIndex(for indexPath: IndexPath?) -> IndexPath? 
        var sectionItems = sections[Int(indexPath?.section ?? 0)]
        var itemIndex: Int? = indexPath?.row
        var subsectionIndex: Int = 0
        for i in 0..<sectionItems.count 
            // First row for each section item is header
            itemIndex = (itemIndex ?? 0) - 1
            // Check if the item index is within this subsection's items
            let subsectionItems = sectionItems[i] as? [Any]
            if (itemIndex ?? 0) < Int(subsectionItems?.count ?? 0) 
                subsectionIndex = i
                break
             else 
                itemIndex = itemIndex! - (subsectionItems?.count)!
            
        
        return IndexPath(row: itemIndex ?? 0, section: subsectionIndex)
    

对于 Objective-C 帮助,您可以关注 URL http://sapandiwakar.in/nested-sections-in-uitableview/

【讨论】:

代码中是否有任何错误:- letsections = [ // 所有部分 [ // 运动部分 [棒球、垒球、板球], // 蝙蝠和球子部分 [Field Hockey, Ice Hockey, Roller Hockey] // Hockey subsection ], [ // Engineering section [Software Engineer, Electrical Engineer] // Computer Science subsection ] ,出现错误。如何解决以及需要粘贴此代码的位置。 给我一分钟 但是你也需要自己创建section headers数组。 但代码中仍然有错误:- 对于 rowItems: [Any] in sectionItems as? [[任何]] ?? [] numberOfRows += rowItems.count // 对于实际表行 这里在 sectionItems 中显示错误,因为 '[Any]' 不是 Array 的子类型 并且单元格中的行也有错误:- var sectionItems = section[indexPath.section] var sectionHeaders = self.sectionHeaders[indexPath.section] 这里显示为错误:- 类型的值没有sectionHeader中的成员。如何解决?

以上是关于如何在tableview中列出数据的主要内容,如果未能解决你的问题,请参考以下文章