且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义单元格的循环加载 Swift

更新时间:2022-12-28 16:50:56

这并不像看起来那么难.因此,在应用程序启动时,您加载初始问题和答案.为此,您需要所需单元格的数量,例如 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.