-3

Hey guys I need to know who to put these array objects into two separate sections, that means one section for the red cell and another section for the blue cell. Would really appreciate your help been stuck on this all day now. Here is my code:

-(void)viewDidLoad {

    [super viewDidLoad];
    //Initialize the array.
    array = [[NSMutableArray alloc] init];
    [array addObject:@"Red"];
    [array addObject:@"Blue"];

    self.ViewTable.backgroundColor = [UIColor clearColor];    
}
Sam
  • 83
  • 1
  • 1
  • 6
  • you only have one array in your code? – Jonathan. Jun 23 '11 at 19:09
  • sorry i meant a different section for each object – Sam Jun 23 '11 at 19:12
  • its cus i have no idea how to delete questions on here :L – Sam Jun 23 '11 at 19:30
  • 2
    AYFKM: "Please may you write the exact code i will need for this to happen thanks." – PengOne Jun 23 '11 at 22:02
  • 1
    Sam - I have merged your two accounts and deleted the duplicate questions. Please DO NOT ask the same question again. Follow by using comments on your question or by editing the question to provide more details about your problem. Thanks. – Kev Jun 23 '11 at 22:36
  • possible duplicate of: [iPhone UITableView sections](http://stackoverflow.com/questions/6445666/iphone-uitableview-sections) – jscs Jun 24 '11 at 04:08

2 Answers2

2

You need to implement:

return number of sections:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

return number of rows for the requested section

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

return correct cell by reading both indexPath.row and indexPath.section

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

So for red you would look for a cell request that has indexPath.section equal to 0 and indexPath.row equal to 0. blue would be indexPath.section equal to 1 and indexPath.row equal to 0

Dancreek
  • 9,524
  • 1
  • 31
  • 34
  • thats great thanks but please may you write the code in full using the objects that i have used. – Sam Jun 23 '11 at 19:19
  • 1
    Sorry, afraid not. There should be enough here to figure it out. Take a look at a couple UITableView tutorials if you need to understand the basics of table programming. – Dancreek Jun 23 '11 at 19:24
1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2; // This returns 2 sections
}

Updated: In the cellForRowAtIndexPath

NSInteger section = [IndexPath section];

if  (section == 0)

  // write your code for red here

if  (section == 1)

  // write your code for blue here
Legolas
  • 12,145
  • 12
  • 79
  • 132
  • that made two sections with the same objects in, I want two sections one with the red object in and one with the blue object in. – Sam Jun 23 '11 at 19:13
  • yes ? So you want 2 sections. 1 section should be red, other 1 should be blue right ? – Legolas Jun 23 '11 at 19:14
  • Yes that is what im looking for – Sam Jun 23 '11 at 19:16
  • hey i did this and it just gave me two sections with red and blue in NSInteger section = [indexPath section]; if (section == 0) [[array objectAtIndex:indexPath.row] isEqual:@"Red"]; // write your code for red here if (section == 1) [[array objectAtIndex:indexPath.row] isEqual:@"Blue"]; – Sam Jun 23 '11 at 19:28
  • I did but i dont know what to write in-between the sections – Sam Jun 23 '11 at 19:32
  • lol. accepting an answer means, clicking that 'tick' mark near my answer. Well.. wat do you mean by write in-between the sections ? – Legolas Jun 23 '11 at 19:36