Как вывести данные в табличном виде - PullRequest
0 голосов
/ 16 ноября 2018

Это снимок экрана. В соответствии с этим, как перечислить данные в табличном представлении:

image

У меня уже есть код, указанный ниже.

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
   }

Это мой код. Но в соответствии с этим я получу вывод, как показано на снимке экрана ниже.

This is the screen shot

На самом деле изначально мне нужно отобразить заголовок раздела как: - Пожалуйста, расскажите о нас

После этого есть подраздел. Заголовок вопросов подраздела приведен ниже: -1. знание 2. дружелюбное или нет

Затем в подразделах отобразить параметры. Как это осуществить?

Ответы [ 2 ]

0 голосов
/ 16 ноября 2018

Вы можете просто отобразить «Пожалуйста, сообщите нам о» как UILabel над таблицей.

0 голосов
/ 16 ноября 2018

Ниже пример может помочь вам.

  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)
    }

А для справки target-C вы можете перейти по следующему URL http://sapandiwakar.in/nested-sections-in-uitableview/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...