ios - 何时使用 numberOfSections 或 numberOfRowsInSection ? ( swift 3)

标签 ios uitableview

enter image description here

为什么? 我不知道为什么输出只给了我 3 个部分。我对 numberOfSections 和 numberOfRowsInSection 感到很困惑。

我不知道的:

为什么输出只返回 3 个部分?

我所知道的:

我在 StackOverFlow 上看到一些帖子,他们说 Sections 就像一个组,而 Rows 就像部分中的单元格。但我还是觉得有点迷茫。

最佳答案

好吧,func numberOfSections(in tableView: UITableView) -> Int//如果没有实现则默认为 1func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> IntUITableViews的数据源方法,numberOfSections在你的TableView中只需要相同类型的重复列表时不需要,比如:假设你想显示在您的表格 View 下方

1

2

3

4

..

在这种情况下,您将需要 numberOfRowsInSection return 4 如果您不提供 numberOfSections 此方法,您的表格将返回 4 个单元格默认情况下将为 1,您的代码将在您的类中没有此方法的情况下工作。

但是假设您需要在您的表格 View 中显示 1个 2个 3个 4个 A 乙 C 丁 可以通过将这些数据作为字符串输入数组并在 cellForAt 中打印来实现。

但是为了使它更有组织性,为了更好地向用户展示,您可以在这里做的是使用部分。喜欢

数量

1

2

3

4

字母表

一个

B

C

D

在这里您必须使用 numberOfSections 并且它应该返回 2,一个用于数字,第二个用于字母。 1、2、3、4 和 A、B、C、D 是它们的数字和字母部分下的行。 我希望你明白了,否则在下面评论我会再试一次让你明白。 谢谢

关于ios - 何时使用 numberOfSections 或 numberOfRowsInSection ? ( swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43339931/

相关文章:

objective-c - 在 iOS 中使用 clock() 函数获取 tics

ios - 在 tableView 中选择多个项目

ios - 如何使表格 View 快速实用地填充整个 View

具有动态高度的 UITableViewCell 中的 iOS 芯片

iphone - 从 tableView 中删除行

ios - tvOS 如何检查 UITabBar 是否聚焦

ios - 带有自定义 header 的 AWSS3GetPreSignedURLRequest 上传,给出 403

ios - 如何使用基于数组的 NSPredicate 过滤核心数据对象

ios - 在后台将数据从 iWatch 发送到 iPhone

ios - 如何在 tableViewCell 中运行 imagePicker