简体   繁体   中英

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. So if lets say

  • if arryData has 3 objects then I want UITable to create 1 row
  • if arryData has 4 objects then I want UITable to create 1 row
  • if arryData has 5 objects then I want UITable to create 2 rows
  • if arryData has 8 objects then I want UITable to create 2 rows
  • if arryData has 10 objects then I want UITable to create 3 rows
  • .... and so on

So how do I do this for N number of objects in my arryData?

    - (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:

- (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.

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)`

 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;

    }`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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