tags:

views:

35

answers:

2

Hi i have a uitableview, i am displaying some data of about 200 rows in it. Data will be displayed in several sections which will be determined at run time. I am able to get sections but i am not able to display data for a particular section in correct order. I have data in the form of dictionaries in an array.

My Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TeraGoAppDelegate *appDel = (TeraGoAppDelegate *)[[UIApplication sharedApplication] delegate];
UITableViewCell *cell = nil;
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    cell.textLabel.text = [(NSMutableDictionary *)[appDel.arrEqp objectAtIndex:countEqpIndex] objectForKey:@"EQP_NAME"];
    cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];
    if(![[(NSMutableDictionary *)[appDel.arrEqp objectAtIndex:indexPath.row] objectForKey:@"select"] isEqualToString:@"0"])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
        cell.accessoryType = UITableViewCellAccessoryNone;
}
// Set up the cell...

return cell;

}

I am trying to use indexPath.row but its values initialises from 0 in every section I can not get data from array in this case as i dont have index of array to get data. How will i get the index of array whose values i need to display????

Update:

[dictComp setObject:arrEqps forKey:CompName];
            [arrCompEqp addObject:dictComp];
            [arrEqps removeAllObjects];

I am using above code to add data in an array for table view but once i remove objects from arrEqps, objects of arrEqps from arrCompEqp are also removed. Why is it not retaining data.

A: 

First of all you have to enqueue and dequeue the cells if you want your table to work smoothly and be able to display huge amounts of data.

Here are few corrections to your code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  TeraGoAppDelegate *appDel = (TeraGoAppDelegate *)[[UIApplication sharedApplication] delegate];

  static NSString *CellIdentifier = @"CellIdentifier";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
    cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];
  }

  // Set up the cell...
  cell.textLabel.text = [(NSMutableDictionary *)[appDel.arrEqp objectAtIndex:countEqpIndex] objectForKey:@"EQP_NAME"];
  if(![[(NSMutableDictionary *)[appDel.arrEqp objectAtIndex:indexPath.row] objectForKey:@"select"] isEqualToString:@"0"])
  {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
  else {
    cell.accessoryType = UITableViewCellAccessoryNone;
  }

  return cell;
}

About the index issue - I don't understand the exact problem.
Usually each table cell represents an item in your datasource array.

What is countEqpIndex?
Why don't you use indexPath.row here too?

Michael Kessler
About the sections, as was already said, you should have a separate array for each section. It might be an array of arrays of dictionaries, when the top array represents the sections, the inner array - cells and the dictionary will hold the data to display in each cell (as you do now).
Michael Kessler
thanks michael for ur reply, i am going ahead with your suggestions. I will let you know when i am done with it.
pankaj
A: 

hi dear I think if you use Switch and choose section as case so in each case u have to put different array so u have to divide your array for each section , this is my opinion i hopes that help you