自定义单元格的循环加载 Swift [英] Cyclic loading of custom cells Swift

查看:30
本文介绍了自定义单元格的循环加载 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直试图从堆栈中获得有关此问题的一些回应,但没有成功.

I've been trying to get some response from stack on this issue for a while now with no success.

所以我会尽量精确.

我有一个带有两个自定义单元笔尖的 uitableview.两者的数据源都是两个单独的数组(问题数组和答案数组).第一个自定义单元格包含 uilabel(问题单元格)和另一个 uibutton(答案单元格).

I have an uitableview with two custom cell nibs. Data source for both are two separate arrays (questions array and answers array). The first custom cell contains uilabel (question cell) and another one uibutton (answer cell).

在应用启动时加载特定数量的问题单元格(取决于数组中的对象数量),然后加载 1 个答案单元格.

On launch of the app a specific amount of question cells load up (depending on the amount of objects in the array) and after them the 1 answer cell loads up.

这是图片

点击 answer2 后 - 再次发生相同的过程,生成下面的新问题单元格以及 answer3 单元格

after click answer2 - again the same procedure happens producing new question cells below as well as the answer3 cell

不知道如何实现.

谢谢!

推荐答案

这并不像看起来那么难.因此,在应用程序启动时,您加载初始问题和答案.为此,您需要所需单元格的数量,例如 3 个问题和 1 个答案,您总共需要从 numberOfCellsInSection: 返回 4.而且,当用户点击(选择,按下)答案行时,您只需增加表格上的单元格数量,如果您在下一个周期有 2 个问题和 1 个答案,现在您将从 numberOfCellsInSection 返回 7: tableView 的数据源方法.等等.

This is not that hard as it seems. So, on the launch of the app, you load the initial questions and answers. For that you would need the number of required cells, say, 3 questions and 1 answer, totally you need to return 4 from the numberOfCellsInSection:. And, when the user taps(selects, press) the answer row, you just increase the number of cells on the table, if you have 2 questions and 1 answer on the next cycle, now you would return 7 from the numberOfCellsInSection: datasource method of the tableView. And so on.

此外,如果您想以更有条理的方式实现目标,您可以将每个问题-答案分成几个部分.在这种情况下,您必须增加表格中的部分数量,并在选择答案单元格时为每个部分返回适当数量的单元格.为此,请使用数据源的 numberOfSectionsInTableview:numberOfCellsInSection 方法.

Also, you can divide the each question-answer into the sections, if you want to have more organized way of achieving your goal. In this case, you have to increase the number of sections in the table, and return proper number of cells for each section on answer cell selection. Use numberOfSectionsInTableview: and numberOfCellsInSection methods of the datasource for this.

不要忘记在 cellForRowAtIndexPath: 方法上绘制(为单元格的标签设置适当的文本,如果有图像)数据以反映单元格上的正确数据.

Don't forget to draw (set proper texts to the cell's labels, image if any) the data on cellForRowAtIndexPath: method to reflect the correct data on your cells.

这篇关于自定义单元格的循环加载 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆