numberOfRowsInSection starts with last section

后端 未结 2 482
再見小時候
再見小時候 2021-01-16 17:52

actually I\'m have a very simple problem (that\'s for sure) I just can\'t find the root cause.

The problem is, that after calling the numberOfSectionsInTableView Met

相关标签:
2条回答
  • 2021-01-16 18:48

    I haven't had my coffee yet but I don't see how this will even compile.

    This line:

    Char *charSpecial = (Char *)[charSpecial objectAtIndex:indexPath.row];
    

    should presumably be:

    Char *charSpecial = (Char *)[charsSpecial objectAtIndex:indexPath.row];
    

    Note the "s". You have the Char type "charSpecial" when you wanted the NSArray "charsSpecial". (These aren't good variable names because they're differentiated only by a single letter buried in the middle. I suggest changing "charsSpecial" to "charSpecials" or better yet "charSpecilsArray".)

    Not sure this is your error because it looks more like a posting typo. You seem to have the same problem here:

    if ([charSpecial count] > 0) {
    
      NSDictionary *charsSpecialDict = [NSDictionary dictionaryWithObject:scodesSpecial forKey:@"Chars"];
      [listOfItems addObject:scodesSpecialDict];
    }
    

    This

    When I call my App, the section title has been set correctly to "#" but the data shows all the "X" values.

    Suggest that somewhere you are swapping your ordinal (zero indexed) value with your cardinal (one indexed). You're correctly querying the section names with an ordinal value but you are fetching your rows with the cardinal. Since the cardinal value is alway one more than the ordinal, you get an out of bounds error.

    I think you should refractor you variable names and look at the code again.

    0 讨论(0)
  • 2021-01-16 18:50

    You've got a big problem with your cellForRowAtIndexPath. CharsSpecial is accessed no matter what section you are in (so long as it has one item), and it may very well not have as many items as that section. This is most likely causing the crash. If your code is well written, then an out of order table shouldn't crash anything.

    IndexPath has two components, section and row. In order to get the data you want out of a 2D array, you should use [[topArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]. Likewise, numberOfRows should use [[topArray objectAtIndex:indexPath.section] count] and numberOfSections should use [topArray count]. This way, all of these methods are consistent. You will need to adapt this for dictionaries, but that should be trivial.

    Once you've done that and you can scroll the table, post a new question about the order of your data, and include all the code that modifies listOfItems.

    Edit:

    ListOfItems seems to only hold the one dictionary, which means you don't really need it. I recommended using an array instead of a dictionary as your top level container because it will make numberOfRows simpler, but to use it, you need to replace, not encapsulate, the dictionary.

    If you want to continue using the dictionary as the top level container that's OK. You should change numberOfSections to count the number of items in the dictionary rather than the listOfItems array. NumberOfRows should look something like this:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        NSString *key = [sectionNameArray objectAtIndex:section];  
        NSArray *array = [dictionary objectForKey:key];
        return [array count];
    }
    

    Where sectionNameArray is an array of strings you use for dictionary keys (they don't have to be the actual section names).

    Edit 2:

    A few things:

    1) alphabetArray has a memory leak, you shouldn't retain it as it's already been retained by alloc.

    2) Any sections that you want to skip need to be removed from the alphabetArray. For this reason it might be better to start with an empty array and add a letter when you get to that if statement (if([charAArray count] >0) for example).

    3) In cellForRow, you seem to be trying to display one item from each letter array all together in each table cell. Is this your intention? The reason you are only seeing the last value is because you keep changing the text of your label (if charAArray has any objects, it overwrites the value charSpecicalArray set). I suspect that what you really want to do is use the code I posted above to select one array, then use the indexPath row to select one item from that array, and set the labels using that one item. Is this correct?

    Edit 3:

    cellForRowAtIndexPath is supposed to return one cell given an indexPath, not all the cells for the table. I don't think you are modifying your letter arrays (you shouldn't be), so what you want is something like this:

    NSString *key = [alphabetArray objectAtIndex:section];  
    NSArray *array = [charDict objectForKey:key];
    Char *myChar = (Char *)[array objectAtIndex:indexPath.row];
    
    cell.textLabel.numberOfLines = 1;
    cell.detailTextLabel.numberOfLines = 2;
    [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize: 12]];
    
    cell.textLabel.text = myChar.ta;
    cell.detailTextLabel.text = myChar.descriptionEN;
    

    This should replace all of the if([array count] >0) sections in cellForRowAtIndexPath

    0 讨论(0)
提交回复
热议问题