其实我有一个非常简单的问题(这是肯定的),我找不到根本原因。numberOfRowsInSection从上一节开始
的问题是,在调用numberOfSectionsInTableView方法后得到(在我的案件24)节数的方法numberOfRowsInSection被调用但最后一节(23)。
滚动时会导致超出边界的索引。
那么,会发生什么? 我收到最后一节(23)的数据,甚至最后一节(23)的行数,之后各节向前移动,如0,1,2,3等...
举例: 我有一个节标题“#”的特殊字符。在我的情况下,最后一部分是以“X”开头的所有值的“X”。
当我打电话给我的应用程序时,节标题已被正确设置为“#”,但数据显示所有“X”值。 此时我有31个“X”值。当我滚动到31行的最后一行时,应用程序因为索引错误而崩溃。
我认为一切都会正常工作如果部分不会从最后一个开始,然后继续第一个(23,0,1,2,3,4等)。 我只是找不到地方或为什么该部分的价值23在第一个地方。
--- EDIT 2开始: 初始化数组:
alphabetArray = [[NSMutableArray alloc] init];
首先,我填补这样一个字典(所有字母含特殊字符。):
charDict = [[NSMutableDictionary alloc] init];
if ([charSpecialArray count] > 0) {
[charDict setObject:charSpecialArray forKey:@"Special"];
[alphabetArray addObject:@"Special"];
}
if ([charAArray count] > 0) {
[charDict setObject:charAArray forKey:@"A"];
[alphabetArray addObject:@"A"];
}
---编辑2结束
所以在这里,我只是用所有“#”值填充第一个字典对象(继续所有其他值)
然后我称之为numberOfSectionsInTableView方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [charDict count];
}
这里numberOfRowsInSection云:
// 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
初始化代码.M:
#import "SCode.h"
@implementation Char
@synthesize ta, report, descriptionDE, descriptionEN;
-(id)initWithTa:(NSString *)t report:(NSString *)re descriptionDE:(NSString *)dde descriptionEN:(NSString *)den {
self.ta = t;
self.report = re;
self.descriptionDE = dde;
self.descriptionEN = den;
return self;
}
- (void)dealloc {
[ta release];
[report release];
[descriptionDE release];
[descriptionEN release];
[super dealloc];
}
@end
嗨大卫, 我改变了这样的代码: numberOfRowsInSection: NSDictionary * dictionary = [listOfItems objectAtIndex:section]; NSArray * array = [dictionary objectForKey:@“SCodes”]; return [array count]; numberOfSectionsInTableView: return [listOfItems count]; cellForRowAtIndexPath: SCode * searchSCodeSpecial =(SCode *)[[searchSCodesSpecial objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 不幸的是我收到以下错误: 2010-02-13 17:15:59.756 SCode [425:207] *** - [NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例0x76f5bc0 – Daniel 2010-02-13 16:23:28
如果我不得不猜测,我会说charSpecial是一个字典,在cellForRowAtIndexPath中你可以调用objectAtIndex。 – 2010-02-13 17:43:05
另外,你可以扩展初始化代码吗?看起来你添加了一个对象到一个字典,只有该字典到你的listOfItems,所以这个表只会有一个对象。它是否正确? – 2010-02-13 17:45:42