Question

I build a Viewcontroller with a TableView for iPhone. Now i try to generate the same content but sorted in a kind of grid. It should be 3 x 3. I generated quads with a measure of 220px x 220px and also set the width of my TableView to 220.

To generate the grid I thought about inserting two more Tableviews which will get 3 specified cells. The problem is just, that I have the method "cellForRowAtIndex" in my ViewController and I can't insert the cell into the tableview I want. I found a method that is called

[self.channelTableView2 insertRowsAtIndexPaths:0    
                              withRowAnimation:UITableViewRowAnimationAutomatic];

But this seems to be the method that adds a new row into a tableview when you're in edit mode.

I also thought about creating a condition that will set 3 specific columns into each tableview but for me it seems like the method cellForRowAtIndexPath is just for one TableView and you can't specify anything inside it.

Does anybody know a way how to solve this Problem? I also saw other posts where a grid has been created but I can't use the old code if I create a new View instead of a new UITableViewCell. And I heard about the Collection View but it is just supported by devices with iOS 6.

Was it helpful?

Solution

I solved the problem. I have 3 tableviews. Their delegate and datasource are connected with my ViewController. Than i created a property for the three tables. Inside my cellForRowAtIndexPath method I detect which tableviewcell will be shown inside a specific tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

MyObject *anObject;
if(tableView == tableview1){

    anObject = [initializedObjects objectAtIndex:indexPath.section * 3];

}else if(tableView == tableview2){

    anObject = [initializedObjects objectAtIndex:indexPath.section * 3 + 1];
}else if(tableView == tableview3){

    anObject = [initializedObjects objectAtIndex:indexPath.section * 3 + 2];
}

I also disabled the scrolling for all tables, so it seems to be a grid. The same detection has to be done inside "didSelectRowAtIndexPath", otherwise you will just select the first three elements inside the object array. (in this case "initializedObjects").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top