简体   繁体   English

uitableview numberOfRowsInSection计数

[英]uitableview numberOfRowsInSection count

This is more of a math problem than anything else. 这比任何其他问题都更是一个数学问题。 What I have is a dynamic array object in which I store user photos. 我所拥有的是一个动态数组对象,用于存储用户照片。

arryData = [[NSArray alloc] initWithObjects:@"pic1.png", @"pic2.png", @"pic3.png", @"pic4.png", @"pic5.png", @"pic6.png",@"pic7.png", @"pic8.png",nil];

This array can have any amount of objects in it eg 8 or 20 or 100. In my table view I have created 4 UIImageViews per row by adding them to cell.contentview. 该数组可以包含任意数量的对象,例如8或20或100。在我的表视图中,通过将它们添加到cell.contentview中,每行创建了4个UIImageViews。 So if lets say 所以如果说

  • if arryData has 3 objects then I want UITable to create 1 row 如果arryData有3个对象,那么我想让UITable创建1行
  • if arryData has 4 objects then I want UITable to create 1 row 如果arryData有4个对象,那么我想让UITable创建1行
  • if arryData has 5 objects then I want UITable to create 2 rows 如果arryData有5个对象,那么我想让UITable创建2行
  • if arryData has 8 objects then I want UITable to create 2 rows 如果arryData有8个对象,那么我想让UITable创建2行
  • if arryData has 10 objects then I want UITable to create 3 rows 如果arryData有10个对象,那么我希望UITable创建3行
  • .... and so on .... 等等

So how do I do this for N number of objects in my arryData? 那么,如何对arryData中的N个对象执行此操作?

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        //NSLog(@"Inside numberOfRowsInSection");
        //return [arryData count];

//Cannot think of a logic to use here? I thought about dividing [arryData count]/4 but that will give me fractions

    }

Picture is worth a thousand words. 图片值一千字。

在此处输入图片说明

So basically you need to divide by four, rounding up. 因此,基本上,您需要除以四,然后四舍五入。 Since integer division in Objective-C truncates (rounds toward zero), you can round up by doing this: 由于Objective-C中的整数除法会截断(四舍五入为零),因此您可以这样做:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return (arryData.count + 3) / 4;
}

In general, to make integer division (with positive integers) round up, you add the denominator-1 to the numerator before dividing. 通常,要对整数除法(带正整数)进行四舍五入,请在除法之前将分母-1加到分子上。

If you have defined a constant for the number of images per row, use it. 如果为每行的图像数定义了一个常数,请使用它。 For example: 例如:

static const int kImagesPerRow = 4;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return (arryData.count + kImagesPerRow - 1) / kImagesPerRow;
}

对于行数,除以图像数并四舍五入:

rowCount = ceilf([arryData count] / 4.0);

I suppose you have only one section so: 我想您只有一节,所以:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   NSInteger photoNumber = [yourDataSource count];
   if(photoNumber % numberOfPhotosOnRow == 0) {
        return photoNumber/numberOfPhotosOnRow;
   }
   else {
        return photoNumber/numberOfPhotosOnRow + 1; 
   }
}

I had to create something similar for an application I wrote a little while back(it seems you want to include 4 images per cell)` 我不得不为我不久前写的一个应用程序创建类似的东西(似乎您想在每个单元格中包含4张图像)`

 if (tableView == yourTableView)
    {
        int rows = [yourArray count];

        int rowsToReturn = rows / 4;
        int remainder = rows % 4;
        if (rows == 0) {
            return 0;
        }
        if (rowsToReturn >0)
        {
            if (remainder >0)
            {
                return rowsToReturn + 1;
            }
            return rowsToReturn ;

        }
        else
            return 1;

    }`

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

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