uitableview numberOfRowsInSection count - iphone

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 e.g 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;
}

For the row count, divide by the number of images and round up:
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;
}`

Related

show a fixed number of cells in every row in a UICollectionView

I want to create fixed number of rows in every cell, and to note is that each section may has multiple number of rows. Say, that there are 20 cells and each row must contain 9 cells in it. So, the number of cells should be 9x3 = 27. 20 cells and remaining 7 cells should be of different colors, there should be three rows then depending on the number of cells. Same thing applied on each section Suppose having three sections then each section will have same story, depending on the number of cells in each section. If anyone has done any such thing, please guide.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(#"vals count:%d", [arrSeats count]);
for(int i=0; i<[arrLevels count]; i++)
{
if(section == i)
{
int c;
NSString *cnt = [NSString stringWithFormat:#"%#",[arrTot objectAtIndex:i]];
c = [cnt intValue];
return c;
}
}
return 1;
}
This is the way i am providing each section different number of cells.
I am answering depending on what I understood from your question. Check if it works for you.
If you have 20 cells(array.count) and you want 9 cells in one row then number of rows will be
numOfRow = 20/9; //2
if(20 % 9 > 0)
numOfRow +=1; //2+1 = 3
numOfCell = numOfRow*9;
Try this if works.

Odd numberOfItems in UICollectionVIew

I am using UICollectionView and my app crashes because I have odd number of items in my list but while I need to tow the items in section.
This is my the numbet of items in every section:
(NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return 2;
}
Here is the problem:
In my list i have 3 items and when the objectAtIndex is 3 the app crashes
MSCoupon *coupon = [list objectAtIndex:indexPath.section * 2 + indexPath.row];
Do you have any solution for me?
This is happening because collection view is trying to access the 2nd element of 2nd section. which resulting in crash because your formula creating index 3.
For second element of section section Your formula
indexPath.section * 2 + indexPath.row
gives
1*2 + 1 = 3
Since your array have 3 element only it will through exception when you try to access fourth element.(array index start with 0).
In Collection view delegate you write this formula. leave rest of your code un-touch it should work
(NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return (list.count/(section + 1)) >= 2? 2: 1;
}
My solution is as follows:
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
//return 2;
return [list count];
}
and:
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
//return [list count]/2;
return 1;
}
Do you have a better advice for me?

custom amount of cells in each tableview group

How would I be able to customize the amount of cells in each tableview group? Basically, I need 2 cells in the first group, 4 cells in the second group, and one cell in the third. I can't seem to find anything, which is odd, unless I'm calling it the wrong thing. Thanks in advance. :)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 2;
else if (section == 1)
return 4;
else if (section == 2)
return 1;
}
Number of cells in each table section is specified in table's data source tableView:numberOfRowsInSection: method. Somewhat dummy example for your case:
- (int) numberOfSectionsInTableView:(UITableView*)tableView{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
int numbers[] = {2,4,1};
return numbers[section];
}

Loading more rows in the tableview

I need to load 15 records initially and then when the user clicks on the show more row of the cell, i need to load the next 20 or available records in my tableview. I am looking at this question posted in SO.
This functionality can be seen in appStore too. Where you load the next number of records.
Here the solution is;
`- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.numberOfRowsVisible;
}
-(void)moreButtonClickAction {
if (self.numberOfRowsVisible < self.maxNumberOfRows)
self.numberOfRowsVisible = MIN(self.numberOfRowsVisible + 10, self.maxRows);
[self.tableView reloadData];
}
I initialized and hard coded the value in the viewDidLoad for numberOfRowsVisible as 15.
self.numberOfRowsVisible=[NSNumber numberWithInt:15];
in numberOfRowsInSection - i am not sure if this is correct.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return MIN([self.arrayRecords count], 15);
}
in the moreButtonClickAction
-(void)moreButtonClickAction {
if (self.numberOfRowsVisible < [self.arrayRecords count])
self.numberOfRowsVisible = MIN(self.numberOfRowsVisible + 15, [self.arrayRecords count]);
[self.tableView reloadData];
}
This is how i edited my code to suit the above suggestion left in SO. but this is not working. There seems to be some issue with MIN. I am not in MAC at the moment, so i can't remember the exact warning/error i got. So could someone kindly help me out.
1) Don't use an NSNumber object, a plain integer will do.
2) Why not just have an integer variable that shows the current number of rows that you want to display, and initialize with 15, and keep it in your class instance.
3) You don't even have to use MIN:
return [self.arrayRecords count] < self.numberOfRowsVisible ? [self.arrayRecords count] : self.numberOfRowsVisible;
You have to use MAX instead of MIN if you want to show all possible entries.
numberOfRowsInSection should return an integer.
self.numberOfRowsVisible is not an integer.
viewDidLoad:
self.numberOfRowsVisible=15;
main implementation:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.numberOfRowsVisible;
}
-(void)moreButtonClickAction {
if (self.numberOfRowsVisible < [self.arrayRecords count]) {
self.numberOfRowsVisible += 15;
if (self.numberOfRowsVisible > [self.arrayRecords count]) {
self.numberOfRowsVisible = [self.arrayRecords count];
}
}
[self.tableView reloadData];
}
off the top of my head, code could do with re-factoring.

Give each cell an unique tag (incremental number)

Is there a way to give each cell in a grouped tableview an incremental number as its tag?
E.g:
Group 1
cell 1 (tag = 1)
cell 2 (tag = 2)
cell 3 (tag = 3)
Group 2
cell 1 (tag = 4)
cell 2 (tag = 5)
Group 3
cell 1 (tag = 6)
etc...
Any help is greatly appreciated!
This is not possible without querying the datasource for the count of cells that are in previous groups. And you probably don't want to do that. Doesn't make sense anyway, because you have to implement proper reuse to get good performance, so tags appear and disappear any time.
So the real question is, why do you want to do this? There is probably a way to achieve the same without adding tags.
But if you really want to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger count = 1;
for (NSInteger i = 0; i < indexPath.section; i++) {
count += [[tableView dataSource] tableView:tableView numberOfRowsInSection:i];
}
count += indexPath.row;
// dequeue, create and configure...
cell.tag = count;
return cell;
}
Create a variable
Create a loop which will iterate through all cells
At the end of each loop add 1 to the variable