【问题标题】:How to make sections with different amount of cells in each [duplicate]如何在每个[重复]中制作具有不同数量单元格的部分
【发布时间】:2017-05-02 03:14:12
【问题描述】:

好的,所以我一直在搜索这个网站以找到解决我的问题的方法。我找到了一个,但由于某种原因,代码无法按预期工作。我的目标是有一个包含部分的表格视图。在每个部分下是每个部分中不同数量的单元格(例如一个部分中的 1 个和另一个部分中的 3 个单元格)。

这是我的代码

import UIKit

class cat1: UIViewController, UITableViewDelegate, UITableViewDataSource {

let proc = [["this"], ["Ye", "it", "will"]]

let infor = [["info 1"] , ["info 3", "info 2" , "info 4"]]
let arrayforsec = [ "test", "testtt"]


override func viewDidLoad() {


    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
       }



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

      return  proc[section].count
           }



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as UITableViewCell
    let cellText = proc[indexPath.section][indexPath.row]
    let descriptiontxt = infor[indexPath.section][indexPath.row]


     cell.detailTextLabel?.numberOfLines = 0
     cell.detailTextLabel?.lineBreakMode = .byWordWrapping

     cell.textLabel?.text = cellText
    cell.detailTextLabel?.text = descriptiontxt

    return (cell)
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section :Int) -> String?{
               return arrayforsec[section]



      }


func numberOfSectionsInTableView(in tableView: UITableView) -> Int {

    return proc.count
}

}

我的目标是拥有 2 个不同的标题,test 和 testtt

test 会有标题 this 和 subtitle info 1 的单元格,以此类推

我在搞砸什么?

这是我咨询的解决方案:How do I populate two sections in a tableview with two different arrays using swift?

(得到 40 人支持的回复)

【问题讨论】:

  • 您的UITableViewDataSourceUITableViewDelegate 方法是否被调用?
  • 是的,我想是的,表格只填充第一部分和单元格,所以:“test”作为部分,“this”作为描述,“info 1”作为副标题,其余的只是不显示...
  • @GaryMakin 如果他将UITableView 添加为视图控制器的子视图而不是使用UITableViewController,则不是这样。
  • @GaryMakin 它是一个视图控制器,其中包含通过情节提要放入的 tableview
  • 当我写我的第一个评论时,我认为代理没有设置,这可能是类错误。当我看到他得到一个部分时,我意识到了问题所在。

标签: ios arrays swift uitableview tableview


【解决方案1】:

您的函数名称错误。应该是:

func numberOfSections(in tableView: UITableView) -> Int {
    return proc.count
}

这是在比您引用的问题更新的 Swift 版本中重命名的。由于 Swift 一直在定期更改,因此值得仔细检查函数和符号的名称。

【讨论】:

  • 是的,就是这样!!!非常感谢,你让我免于很多头痛
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多