0

i need to have a 61 section in UITableView, thats mean i must have 61 NSArray, right ?!

first i define 61 NSArray, and init that in view did load

it's tired to use this code

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    switch (section) {
        case 0: 
           return [searchArray_1 count]; 
           break;
        case 1: 
           return [searchArray_2 count]; 
           break;
        case 2: 
           return [searchArray_3 count]; 
           break;
        case 3: 
           return [searchArray_4 count]; 
           break;
          ...
        case 60: 
           return [searchArray_61 count]; 
           break;
    }
    return 1;
 }

and this code in configure cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:CellIdentifier];

    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = [searchArray_1 objectAtIndex:indexPath.row];
            break;
        case 1:
            cell.textLabel.text = [searchArray_2 objectAtIndex:indexPath.row];
            break;
        case 2:
            cell.textLabel.text = [searchArray_3 objectAtIndex:indexPath.row];
            break;
           ...
        case 60:
            cell.textLabel.text = [searchArray_61 objectAtIndex:indexPath.row];
            break;

        default:
            break;
    }

    return cell;
}

it's working ok, but how can i reduce that arrays

Almudhafar
  • 887
  • 1
  • 12
  • 26
  • 1
    Why not use a dictionary instead {@"key":object}? – Joshua Jan 15 '14 at 08:35
  • possible duplicate of [What would be a good data structure for UITableView in grouped mode](http://stackoverflow.com/questions/20216902/what-would-be-a-good-data-structure-for-uitableview-in-grouped-mode) – Martin R Jan 15 '14 at 08:35
  • you can also user Dictionary.that into store all Array.so,u can simply Access particular array using key and value pair.I think it's best approach... – Renish Dadhaniya Jan 15 '14 at 08:46

4 Answers4

3

You need anther array.

NSArray * sectionArray = @[searchArray_1, searchArray_2, ...., searchArray_61] ;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSArray * array = sectionArray[section] ;
    return [array count] ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:CellIdentifier];
    NSArray * array = sectionArray[indexPath.section] ;
    cell.textLabel.text = array[indexPath.row] ;
    return cell;
}
KudoCC
  • 6,912
  • 1
  • 24
  • 53
0

You could make an NSDictionary with all your arrays included e.g. (Assuming you have a property of type NSDictionary with the name database);

self.database = @{ @1 : myArray1, @2:myArray2:};

you still have to fill it the first time by hand. But in your cellForRowAtIndexPath: you could then do something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSArray *arrayToUse = self.database[@(indexPath.row)];
    UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.textLabel.text = arrayToUse[indexPath.row];
    return cell;
}

So you would use the indexPath.row to lookup the correct NSArray in you NSDictionary.

Pfitz
  • 7,336
  • 4
  • 38
  • 51
  • it's not work probably result in section 1: first object from array 1, and second object from array 2 and the section 2 is same – Almudhafar Jan 15 '14 at 09:01
0

Use Plist Database.plist database into make Dictionary.that into store all Array.so,u can simply Access particular array using key and value pair.I think it's best approach...

Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
0

You can use dictionary for this. In your case You need 61 key value pairs. For getting 61 sections You can use

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [dictionary allKeys].count;
}

And to get required number of rows in each section implement following method.

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

}

To fill all the cells with values implement following delegate method.

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

}

Here to get correct data into the table You need to point the values of particular key based on the section.

Surendra
  • 201
  • 3
  • 6