I have an array with 20 objects. there can be more objects in that array. for simplicity reason lets say its only nsstring object in that array.
i want to show 3 of those elements in every row. so number of rows are
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int i = 0;
if ( ([myArray count] % 3) > 0 )
{
i++;
}
return [myArray count] / 3 + i;
}
i have a helper veriable int lastObj=0
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
---instaniate the cell
for (int i = 1; i <= 3; i++)
{
if (lastObj <= [myArray count])
{
--create cell content
-- cellContent.myLabel.text=[myArray onbjectAtIndex:lastObj]
--add cellContent to cell
lastObj++;
}
}
return cell;
}
so if i have 5 objects in that array, then they get displayed properly.
but if it the list has 14 elements then the first 9 elements gets display and it starts from element 0 and the rest dont show up. on the app you can see 3 rows and each have 3 elements of array.
so i’m trying to mimic 3 columns.
any idea how i can resolve this issue?
Try this:
The Point is that cellForRowAtIndexPath is not nessessarily called in a certain sequence. It is rather coincidence that it is called for the first to seventh row from start. Theoretically it could be called in any sequence.
Think of this:
Your table has 30 rows. 10 of thes are visible at once.
At the beginning it is called for row 0 to 9. But understand that as a coincidence. It could be called in any sequence.
Then the user scrolls for 5 rows. The method is then called for cell 10 to 14. In this case the cells will be re-used. Your
if (cell==nil)branch will not be entered.Again in a row. Then the user crolls back 2 rows. Next calls of the method would be for row 9 and 8 – in that sequence.
So do never ever assume a certain sequence here, even if you seem to observe a certain sequence.