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;
Related
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;
}`
I want to do somenthing with the UIPickerView but I simple can't see how I can solve this problem...
This example is similar to what I want, and easily understandable. I have two simple pickers, with numbers from 0 to 10. Then I have a textfield where the user inserts a number.
What I want is that when the users inserts, for example, 7, and then selects a number in one of the pickers, like 5, the other pickers moves to 2, so that the sum is 7. The same with 0 and 7, 3 and 4.
Basically, how to make a picker move, according to the other picker value selected, based on the input inserted before.
Thank you very much :)
u add array and reload component like this:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
club=[[NSString alloc] initWithFormat:#"%#" , [Nations objectAtIndex:row]];
[secondcomponearray addObject club];
[pickerView reloadComponent:1];
}
try it out suppose you have two UIPickerView like pickerview1 and pickerview2 then you can set the logic like bellow..
Note 1.:- If you used different 2 UIPickerView then use bellow code..
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (thePickerView == pickerview1) {
if ([yourTextfield.text integerValue] > row) {
int selectVal = [yourTextfield.text integerValue] - row ;
[pickerview2 selectRow:selectVal inComponent:0 animated:YES];
}
else{
int selectVal = row - [yourTextfield.text integerValue] ;
[pickerview2 selectRow:selectVal inComponent:0 animated:YES];
}
}
}
Here what you get just see here if you entered value in UITextField 7 and then select value 5 of pickerview1 then its row number is 4.
here when you minus that 4 from the 7 then what you get is 3 after you set or Select the row number is 3 of pickerview2 and its value is 2.
Note 2.:- If you used 1 UIPickerView and 2 components then use bellow code..
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
if ([yourTextfield.text integerValue] > row) {
int selectVal = [yourTextfield.text integerValue] - row ;
[thePickerView selectRow:selectVal inComponent:1 animated:YES];
}
else{
int selectVal = row - [yourTextfield.text integerValue] ;
[thePickerView selectRow:selectVal inComponent:1 animated:YES];
}
}
}
So you got exact output which you want..
i hope you got it...
Enjoy the coding.. :)
Code modified from this post: find pair of numbers in array that add to given sum
The code below loops through to find the possible pairs adding up to the number (the number typed into the textfield), and then in the picker selects the rows with the numbers equalling one of the numbers in the pair. The code assumes that your data source contains the numbers from 0 up to some total in ascending order.
NSInteger number = [[textField text] integerValue];
int i = 0;
int j = number-1
NSMutableArray *possiblePairs = [NSMutableArray array];
while(i < j){
if (i + j == number) {
possiblePairs[] = #[(i), #(j)];
}
else if (a[i] + a[j] < number) i += 1;
else if (a[i] + a[j] > number) j -= 1;
}
[picker1 selectRow:[dataSource indexOfObject:#(i)] inComponent:0];
[picker2 selectRow:[otherDataSource indexOfObject:#(j)] inComponent:0];
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.
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?
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