【问题标题】:Segmented Control inside a TableViewTableView 中的分段控件
【发布时间】:2017-07-29 17:42:58
【问题描述】:

我花了几个小时试图找到解决方案,但我总是一无所获。

我正在尝试在 TableView 内创建一个段控件,该段控件具有一个 CustomCell 用于段控件的两个视图。

https://www.youtube.com/watch?v=dw7kytoA3KU&t=198s 是我想要完成的一个完美示例,但用户插入了 1 个 CustomCell 并且代码有点混乱。

我的第一个 CustomCell 包含图像、名称和数字。 我的第二个 CustomCell 只包含一个图像。

所以我的问题是......

  1. 我应该如何为两个CustomCell创建两个数组?

    我正在尝试完成的示例

自定义单元格 1:

              let names = ["Pizza","Soup","Tacos","Chicken", "Beef"]
              let image = [UIImage(named: "Pizza1"), UIImage(named: "Soup1"), UIImage(named: "Tacos1"), UIImage(named: "Chicken1"), UIImage(named: "Beef1")]
              let numbers = ["1", "2", "3", "4", "5"]

自定义单元格 2:

          let photo = [UIImage(named: "Pic1"), UIImage(named: "Pic2"), UIImage(named: "Pic3")]
  1. 根据我的研究,当需要将 CustomCell 文件和两个 CustomCell 调用到我的代码中时,这是否正确?

     let nib = UINib(nibName: "CustomCell", bundle: nil)
     tableView.register(nib, forCallReuseIdentifier: "CustomCell1")
     tableView.register(nib, forCallReuseIdentifier: "CustomCell2")
    
  2. 为此应该返回什么?

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

  3. 这部分是我对输入内容感到困惑的地方

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

总结:我正在尝试在具有 2 个 CustomCells 的 tableView 中创建一个段控件。我希望该段的第一个视图是具有图像、名称和编号的 CustomCell。该段的第二个视图只有图像。

谢谢!

【问题讨论】:

  • 一个TableView...里面...一个段控件?
  • @Matthew ...我的错,Tableview 中的 UISegmented 控件

标签: arrays swift tableview uisegmentedcontrol custom-cell


【解决方案1】:

我应该如何为两个CustomCell创建两个数组?

let names = ["Pizza","Soup","Tacos","Chicken", "Beef"]
let image = [UIImage(named: "Pizza1"), UIImage(named: "Soup1"), UIImage(named: "Tacos1"), UIImage(named: "Chicken1"), UIImage(named: "Beef1")]
let numbers = ["1", "2", "3", "4", "5"]

拥有三个数组会起作用,但有点奇怪。一个 Swift 结构和一个数组可能更容易使用:

struct Food {
  var name: String
  var image: UIImage
  var number: Int
}

let foods = [
  Food(name: "Pizza", image: UIImage(named: "Pizza1"), number: 1),
  Food(name: "Soup", image: UIImage(named: "Soup1"), number: 2),
  Food(name: "Tacos", image: UIImage(named: "Tacos1"), number: 3),
  Food(name: "Chicken", image: UIImage(named: "Chicken1"), number: 4),
  Food(name: "Beef", image: UIImage(named: "Beef1"), number: 5)
]

根据我的研究,当需要将 CustomCell 文件和两个 CustomCell 调用到我的代码中时,这是否正确?

let nib = UINib(nibName: "CustomCell", bundle: nil)
tableView.register(nib, forCallReuseIdentifier: "CustomCell1")
tableView.register(nib, forCallReuseIdentifier: "CustomCell2")

几乎...如果您有两个自定义单元格,我希望看到两个笔尖:

let nib1 = UINib(nibName: "CustomCell1", bundle: nil)
tableView.register(nib1, forCallReuseIdentifier: "CustomCell1")
let nib2 = UINib(nibName: "CustomCell2", bundle: nil)
tableView.register(nib2, forCallReuseIdentifier: "CustomCell2")

为此应该返回什么?

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

表示你表格中的单元格数。所以它可能是:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return foods.count
}

这部分是我对输入内容感到困惑的地方

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

这是您的应用程序出列一个单元格的地方,并在它出现在表格中之前对其内容进行配置:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell1", for: indexPath)
  cell.nameLabel.text = foods[indexPath.row].name
  return cell
}

我仍然不确定您打算对 SegmentedControls 做什么...您是否希望每个单元格都包含一个分段控件?那将是相当……不寻常的。

【讨论】:

    【解决方案2】:

    首先我建议你阅读一些关于如何使用 UITableViews 和 UISegmentedControl 的教程,因为看起来你是 iOS 开发的初学者。现在回答您的问题:

    #1. How should I create two array for the two CustomCell?
    

    我建议你学习在 Swift 中创建 NSObjects。使用它,您可以在对象上设置属性,例如名称、图像、数字等。然后,您可以收集对象并将它们用于表的数据源。

    #2. Regarding Custom UITableViewCells.
    

    是的,您需要注册您的自定义 UITableViewCells 才能使用它们。或者做故事板的方式。

    #3. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    

    您在此委托方法中指定应出现在 UITableView 中的行数。例如,如果您有一个 food 对象数组,您将返回 foods.count。

    #4. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    

    这是您创建和自定义将显示在 UITableView 行上的单元格的部分。

    如果你想同时使用 UITableView 和 UISegmentedControl,我建议采用最简单的方法,在 UIViewController 的视图上添加一个 UISegmentedControl 和两个 UITableView。根据选中的段来改变可见的 UITableView,你可以在 UISegmentedControl 的值改变事件上捕获它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多