Ios tableview如何查找一节中的行数?

时间:2014-06-05 07:15:45

标签: ios uitableview

我有一个动态数组。我需要在下面的场景中显示tableview ... 在第一个单元格中,我需要显示1个项目。 在第二个单元格中,我需要显示2个项目。 在第三个单元格中,我需要显示3个项目。 在第四个单元格中,我需要显示1个项目。 在第五个单元格中,我需要显示2个项目。 在第六个单元格中,我需要显示3个项目。 等等... 任何人都可以建议如何在一个部分中不返回任何行。

enter image description here

3 个答案:

答案 0 :(得分:2)

试试这个:

int noOfRow = total/2 + ceil((total % 3)/3.0);

答案 1 :(得分:1)

简单的逻辑是:

NoOfRows = TotalCount / 2

例如:

If last value is 6 then, total no of rows are (6 / 2) = 3
If last value is 12 then, total no of rows are (12 / 2) = 6

你必须认为是合乎逻辑的。

希望这有帮助。

答案 2 :(得分:1)

更快的方法可能是:

注意在除以2的方法中,大多数数字都有效。那些不起作用的是: 2,4,8,10 ......基本上是偶数不能被6整除的数字。

所以我们可以提出类似的东西:

int count = array.count;
if (count % 2 == 0 && count % 6 != 0) {
    count + 2;
}
int rows = ceilf(count / 2);

或者我们可以写一个for循环:

int counter = array.size;
int rows = 0;
int dec = 1;

while (counter > 0) {
    rows++;
    counter - dec;
    dec = dec % 3 + 1;
}

for循环当然更慢。

相关问题