Reputation: 1816
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?
Upvotes: 0
Views: 1947
Reputation: 14068
Try this:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
---instaniate the cell
cell = [[UITableViewCell alloc] init]; //If you don't ARC then retain accordingly.
}
for (int i = indexPath.row * 3 ; i < indexPath.row * 3 + 3; i++)
{
if (i <= [myArray count])
{
--create cell content
-- cellContent.myLabel.text=[myArray onbjectAtIndex:i]
--add cellContent to cell
//forget about your last object helper. That would not work anyway.
}
}
return cell;
}
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.
Upvotes: 0
Reputation: 2032
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ceil(((float)[myArray count]) / 3.0);
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
---instaniate the cell
}
//initiate 3 labels, then say:
if(myArray.count-1 >= IndexPath.row * 3)
cellContent.myLabel_1.text=[myArray onbjectAtIndex:IndexPath.row * 3];
if(myArray.count-1 >= (IndexPath.row * 3)+1)
cellContent.myLabel_2.text=[myArray onbjectAtIndex:(IndexPath.row * 3)+1];
if(myArray.count-1 >= (IndexPath.row * 3)+2)
cellContent.myLabel_3.text=[myArray onbjectAtIndex:(IndexPath.row * 3)+2];
//the above "if"-s are to prevent reading values out of the array's bounds
//add labels to cell
return cell;
}
Upvotes: 2
Reputation: 617
There is currently only 1 text label in the cell you are trying to insert 3 text labels in. You seem to be close but I think the best option is to create a custom table cell with three text labels and then set them as you are currently doing here is a tutorial to setup custom tabel cells: http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Upvotes: 0