简体   繁体   English

自定义单元的循环加载Swift

[英]Cyclic loading of custom cells Swift

I've been trying to get some response from stack on this issue for a while now with no success. 我一直在尝试从堆栈上就此问题获得一些响应,但至今没有成功。

So i will try to be very precise. 所以我会尽量精确。

I have an uitableview with two custom cell nibs. 我有一个带有两个自定义单元笔尖的uitableview 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). 第一个自定义单元格包含uilabel (问题单元格)和另一个uibutton (答案单元格)。

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. 在启动该应用程序时,将加载特定数量的问题单元格(取决于数组中的对象数量),之后将加载1个答案单元格。

Here is the pic 这是照片

在此处输入图片说明

after click answer2 - again the same procedure happens producing new question cells below as well as the answer3 cell 单击answer2之后-再次发生相同的过程,并在下面生成新的问题单元格以及answer3单元格

Don't know how to achieve that. 不知道如何实现。

Thank you! 谢谢!

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: . 为此,您需要一定数量的单元格,例如3个问题和1个答案,总共需要从numberOfCellsInSection:返回4个。 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 . 而且,当用户点击(选择,按)答案行时,您只需增加表格中的单元格数量,如果在下一个周期中有2个问题和1个答案,现在您将从numberOfCellsInSection: datasource方法返回7 tableView And so on. 等等。

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. 为此,请使用数据源的numberOfSectionsInTableview:numberOfCellsInSection方法。

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. 不要忘记在cellForRowAtIndexPath:方法上绘制(在单元格的标签上设置适当的文本,如果有的话设置图像)数据以反映单元格上的正确数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM