ios - 如何在每个切片中制作不同数量的细胞

标签 ios arrays swift uitableview tableview

<分区>

好的,所以我一直在搜索此站点以找到解决我的问题的方法。我找到了一个,但出于某种原因,代码无法按预期方式工作。我的目标是拥有一个包含部分的表格 View 。在每个部分下是每个部分中不同数量的单元格(例如一个部分中有 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 个不同的 header ,test 和 testtt

测试将有标题为 this 和副标题信息为 1 的单元格,依此类推

我搞砸了什么?

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

(有 40 个赞成票的回应)

最佳答案

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

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

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

关于ios - 如何在每个切片中制作不同数量的细胞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43729485/

相关文章:

ios - Redux:最佳实践(使用 Swift)

ios - 我在一个 View Controller 上有两个 TableView,如何根据被点击的 TableView 将数据传递给单独的 View Controller?

ios - 如何禁用选项卡栏项目的选择 - swift

java - 循环导致数组旋转超时

用于遍历目录/文件树和输出树作为嵌套 UL 的 PHP 脚本

ios - 如何增加Xcode中导航栏的高度?

ios - CollectionView Flowlayout自定义

ios - 解析保存指针

iphone - 手动调用 applicationDidEnterBackground

javascript - 使用数组中的名称创建新的(全局)对象