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

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.

Related

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 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;
}`

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?

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

indexpath of cells in grouped tableview

i have a table view, split into alphabetical sections, populated from array that has been filtered
how can i track the "true" indexpath of cells in a grouped tableview.
instead of the count starting at 0 for each section.
i need to get this so i can access the right bit of data from an array
at the moment it works like this(just in case im not clear)
(section 0)
row 0
row 1
row 2
(section 1)
row 0
row 1
etc...........
i need to get it so i can get
section 0)
row 0
row 1
row 2
(section 1)
row 3
row 4
thanks in advance
Try this:
- (NSUInteger)indexFromIndexPath:(NSIndexPath*)indexPath
{
NSUInteger index=0;
for( int i=0; i<indexPath.section; i++ )
index += [self tableView:self.tableView numberOfRowsInSection:i];
index += indexPath.row;
return index;
}
It assumes that this method is placed in a UITableViewController class.
You can use it like this:
NSUInterger realIndex = [self indexFromIndexPath:indexPath];
id myObject = [dataArray objectAtIndex:realIndex];
Use this code to get correct row,
NSUInteger row = 0;
for (NSUInteger i = 0; i < noofsectionsinTableView; i++)
row += [self.tableView numberOfRowsInSection:i];
return row;

sectionIndexTitlesForTableView line up correctly with sections

I've got a table view with many sections, the title for these sections is just A-Z and # just like in the iPhone address book App. I have implemented sectionIndexTitlesForTableView to have the quick move to particular letter and basically just return an array of Letters A - Z and #.
This would work if my list always contains an item for every letter of the alphabet but it won't and this screws up the section index titles because hitting C in the list might go to D if the 3th section is D (ie if there is nothing in section C).
I know I could return the array in sectionIndexTitlesForTableView with only the letters that are sections but this would look a bit odd and not the same functionality as the iPhone Address book app.
How can I rectify this?
I don't see how #Rudiger's method would work if you have only have sections A, C, F, S, T and section index titles for A-Z. Such a situation might arise when using MPMediaQuery.
To get around this I've implemented this method as follows, the tableview will scroll to the correct section or the next if the one you are looking for doesn't exist.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSString *sectionTitle = nil;
NSComparisonResult result;
int i;
for(i = 0; i < tableView.numberOfSections; i++)
{
sectionTitle = [self tableView:tableView titleForHeaderInSection:i];
result = [title compare:sectionTitle];
if(result != NSOrderedDescending)
break;
}
return (MIN (i, (tableView.numberOfSections - 1)));
}
UPDATE
Changed the return value to fix the situation described by Eric D'Souza.
Basically you have to implement:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [sections indexOfObject:title];
}
and return based on the index and title what section it should be on. Where sections is the array storing the list of sections