Iphone numberOfRowsInSection从最后一节开始

Iphone numberOfRowsInSection从最后一节开始,iphone,Iphone,事实上,我有一个非常简单的问题(这是肯定的),我就是找不到根本原因 问题是,在调用numberOfSectionsInTableView方法以获取节数(在我的例子中为24)之后,会调用numberOfRowsInSection方法,但最后一节(23)会被调用 这会在滚动时导致索引越界 那么,会发生什么? 我正在接收最后一部分(23)的数据,甚至是最后一部分(23)的行数,然后这些部分向前移动,如0、1、2、3等 例如: 对于特殊字符,我有一个标题为“#”的节。在我的例子中,最后一部分是以“X”开

事实上,我有一个非常简单的问题(这是肯定的),我就是找不到根本原因

问题是,在调用numberOfSectionsInTableView方法以获取节数(在我的例子中为24)之后,会调用numberOfRowsInSection方法,但最后一节(23)会被调用

这会在滚动时导致索引越界

那么,会发生什么? 我正在接收最后一部分(23)的数据,甚至是最后一部分(23)的行数,然后这些部分向前移动,如0、1、2、3等

例如: 对于特殊字符,我有一个标题为“#”的节。在我的例子中,最后一部分是以“X”开头的所有值的“X”

当我调用我的应用程序时,节标题已正确设置为“#”,但数据显示所有“X”值。 此时,我有31个“X”值。当我现在滚动到31行中的最后一行时,由于索引错误,应用程序崩溃

我认为如果不是从最后一节开始,然后再从第一节开始(23,0,1,2,3,4等),一切都会正常工作。 我就是找不到第一个地方的值是23,或者为什么这个部分的值是23

---编辑2开始: 初始化数组:

    alphabetArray = [[NSMutableArray alloc] init];
首先,我填写一本这样的字典(所有字母,包括特殊字符):

---编辑2个端点

因此,这里我只是用所有“#”值填充第一个dictionary对象(继续填充所有其他值)

然后我调用numberOfSectionsInTableView方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return [charDict count];
}
下面是NumberOfRowsin部分:

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

NSString *key = [alphabetArray objectAtIndex:section];  
NSArray *array = [charDict objectForKey:key];
return [array count];
 }

}
以及cellForRowAtIndexPath:

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

  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if(cell == nil)
  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];

if ([charSpecialArray count] > 0)   {

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

    cell.textLabel.numberOfLines = 1;
    cell.detailTextLabel.numberOfLines = 2;
    [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize: 12]];

    cell.textLabel.text = charSpecial.ta;
    cell.detailTextLabel.text = charSpecial.descriptionEN;
}

if ([charAArray count] > 0) {

    Char *charA = (Char *)[[charAArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    cell.textLabel.numberOfLines = 1;           
            cell.detailTextLabel.numberOfLines = 2;
    [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize: 12]];

    cell.textLabel.text = charA.ta;
    cell.detailTextLabel.text = charA.descriptionEN;

    }
再次为所有角色

有什么建议可以解决这个问题吗

增加 初始化代码.h:

#import <UIKit/UIKit.h>

@interface Char : NSObject {
NSString *ta;
NSString *report;
NSString *descriptionDE;
NSString *descriptionEN;
}

@property (nonatomic, retain) NSString *ta;
@property (nonatomic, retain) NSString *report;
@property (nonatomic, retain) NSString *descriptionDE;
@property (nonatomic, retain) NSString *descriptionEN;


-(id)initWithTa:(NSString *)t report:(NSString *)re descriptionDE:(NSString *)dde     descriptionEN:(NSString *)den;

@end

我还没有喝咖啡,但我不知道这会是怎样编译的

这一行:

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

Char *charSpecial = (Char *)[charsSpecial objectAtIndex:indexPath.row];
注意“s”。当您需要NSArray“charsSpecial”时,您有Char类型“charSpecial”。(这不是一个好的变量名,因为它们只在中间被一个字母所区分。我建议把“ChansSuffic”改为“ChanExrices”或更好的“CabScript数组”。 不确定这是你的错误,因为它看起来更像是一个发布错误。你似乎也有同样的问题:

if ([charSpecial count] > 0) {

  NSDictionary *charsSpecialDict = [NSDictionary dictionaryWithObject:scodesSpecial forKey:@"Chars"];
  [listOfItems addObject:scodesSpecialDict];
}
这个

当我调用我的应用程序时,节标题已正确设置为“#”,但数据显示所有“X”值

建议在某个地方将序号(零索引)值与基数(一索引)值交换。您使用序号值正确地查询了节名称,但使用基数获取行。由于基数值总是比序数多1,因此会出现越界错误


我认为您应该折射变量名,然后再看一遍代码。

我还没有喝咖啡,但我不知道这将如何编译

这一行:

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

Char *charSpecial = (Char *)[charsSpecial objectAtIndex:indexPath.row];
注意“s”。当您需要NSArray“charsSpecial”时,您有Char类型“charSpecial”。(这不是一个好的变量名,因为它们只在中间被一个字母所区分。我建议把“ChansSuffic”改为“ChanExrices”或更好的“CabScript数组”。 不确定这是你的错误,因为它看起来更像是一个发布错误。你似乎也有同样的问题:

if ([charSpecial count] > 0) {

  NSDictionary *charsSpecialDict = [NSDictionary dictionaryWithObject:scodesSpecial forKey:@"Chars"];
  [listOfItems addObject:scodesSpecialDict];
}
这个

当我调用我的应用程序时,节标题已正确设置为“#”,但数据显示所有“X”值

建议在某个地方将序号(零索引)值与基数(一索引)值交换。您使用序号值正确地查询了节名称,但使用基数获取行。由于基数值总是比序数多1,因此会出现越界错误


我认为您应该折射变量名并重新查看代码。

您的
cellForRowAtIndexPath
有一个大问题。CharsSpecial无论您在哪个部分都可以访问(只要它有一个项目),并且它可能没有该部分那么多项目。这很可能是导致坠机的原因。如果您的代码编写得很好,那么无序表就不会崩溃

IndexPath有两个组件,节和行。为了从2D数组中获取所需的数据,应该使用
[[topArray objectAtIndex:indexath.section]objectAtIndex:indexath.row]
。同样,numberOfRows应该使用
[[topArray objectAtIndex:indexPath.section]计数]
,numberOfSections应该使用
[topArray计数]
。这样,所有这些方法都是一致的。您需要将其应用于词典,但这应该是微不足道的

完成后,您可以滚动表格,发布关于数据顺序的新问题,并包括修改ListoItems的所有代码

编辑:

列表项似乎只包含一本字典,这意味着你并不真的需要它。我建议使用数组而不是字典作为顶级容器,因为它将使numberOfRows更简单,但要使用它,需要替换而不是封装字典

如果您想继续使用字典作为顶级容器,那没关系。您应该更改numberOfSections以计算字典中的项数,而不是ListoItems数组中的项数。NumberOfRows应该如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key = [sectionNameArray objectAtIndex:section];  
    NSArray *array = [dictionary objectForKey:key];
    return [array count];
}
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;
其中
sectionNameArray
是用于字典键的字符串数组(它们不必是实际的节名)

编辑2:

有几件事:

1) alphabetArray存在内存泄漏,您不应该保留它,因为它已被alloc保留

2) 需要从alphabetArray中删除任何要跳过的部分。因此,最好从一个空数组开始,然后添加一个字母w