0

In My Expandable UITableview number of section are 5 [SectionItems.count].

My goal is to number all cells in the sections from 1 to count of all rows (numbering should not respect sections).

enter image description here

this is my code for count rows

NSInteger count = 0;
for (NSInteger sec=0; sec < indexPath.section; sec++) {
    NSInteger rows = [tableView numberOfRowsInSection:sec];
    count += rows;
}
count += indexPath.row + 1;


NSArray *sect = [sectionItem objectAtIndex:indexPath.section];
cell.titleLbl.text = [NSString stringWithFormat:@"%ld %@",(long)count,[sect objectAtIndex:indexPath.row]];

But I got what you can see in next image:

enter image description here

The problem is that the first section (Versioning scheme) has a row, so those two numbers should be 2 and 3 instead of 1 and 2.

What am I doing wrong here?

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
Ketan Odedra
  • 1,215
  • 10
  • 35
  • can you show the format of array ?. Get video index of the object and display if it is a single array. – Vinodh Jun 26 '18 at 11:02

2 Answers2

1

The problem here must be that you check all currently visible rows. You should create one more array with cell numbers and then get them the same as you get text.

Each time you update row data you should redo the numbering

- (NSArray *)numberCells {
    NSArray *numbersArray = [[NSArray alloc] init];
    NSInteger num = 1;
    for (NSArray *ar in sectionItem) {
        NSArray *rowArray = [[NSArray alloc] init];
        for (id item in ar) {
            rowArray = [rowArray arrayByAddingObject:[NSNumber numberWithInteger:num]];
            num += 1;
        }
        numbersArray = [numbersArray arrayByAddingObject:rowArray];
    }
    return numbersArray;
}

Update array property when needed like this: myArray = [self numberCells]; then get cell number like this:

NSArray *rowArray = [numbersArray objectAtIndex:indexPath.section];
NSNumber *num = [rowArray objectAtIndex:indexPath.row];

Good luck!

Yaroslav Luchyt
  • 223
  • 3
  • 8
1

I would do some structure like an array of sections, and every section should have a title and an array of rows. In JSON style, something like:

[
    {
        "section_title": "My section 1",
        "section_rows": [
            {"title": "Lecture 1"},
            {"title": "Lecture 2"}
        ]
    },
    {
        "section_title": "My section 2",
        "section_rows": [
            {"title": "Lecture 3"},
            {"title": "Lecture 4"}
        ]
    }
]

With that, your methods would be something like:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return myArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    __weak NSDictionary *section = myArray[section];
    return [section[@"section_rows"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak NSDictionary *lecture = myArray[indexPath.section][@"section_rows"][indexPath.row];
    // Configure your cell here
}

// This method should probably be replaced with this one from UITableViewDelegate:
// - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    __weak NSDictionary *sectionInfo = myArray[section];
    return sectionInfo[@"title"];
}

Instead of making a mess trying to count/access your data, you should pre-format it to an easy to handle structure before sending/showing it on your UIViewController. Don't just get your UIViewController dirty because of data, it should be the other way around and your view should be passive.

I hope this is what you're asking for, I'm not quite sure what you mean by "expandable" table view.

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30