실제로 저는 근본 원인을 찾을 수없는 매우 간단한 문제가 있습니다.numberOfRowsInSection은 마지막 부분으로 시작합니다.
문제는 NumberOfSectionsInTableView 메서드를 호출하여 Sections 수 (본인의 경우 24)를 얻은 후에 numberOfRowsInSection 메서드가 호출되지만 마지막 섹션 (23)이 호출된다는 것입니다.
이렇게하면 스크롤 할 때 범위를 벗어나는 인덱스가 생성됩니다.
그래서 어떻게됩니까? 마지막 섹션 (23)의 데이터와 마지막 섹션 (23)의 행 수를받은 후 섹션이 0,1,2,3 등과 같이 앞으로 이동합니다.
예 : 특수 문자에 대한 섹션 헤더가 "#"인 섹션이 있습니다. 필자의 경우 마지막 섹션은 "X"로 시작하는 모든 값의 "X"입니다.
내 앱을 호출 할 때 섹션 제목이 "#"으로 올바르게 설정되었지만 데이터에 "X"값이 모두 표시됩니다. 이 시점에서 저는 31 "X"값을가집니다. 이제 31 행의 마지막으로 스크롤 할 때 인덱스 오류가 발생하여 앱이 다운됩니다.
나는 모든 것이 올바르게 작동 할 것이라고 생각한다. 섹션이 마지막 것에서 시작하지 않을 것이고 이후에 첫번째 것 (23,0,1,2,3,4, etc.)을 계속할 것이라고 생각한다. 섹션 23 또는 23에서 첫 번째 장소의 값이 가장 높은 장소를 찾을 수 없습니다.
은 --- 편집 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
그래서 여기에 모든 "#"값 (다른 모든 값으로가는)으로 첫 번째 사전 개체를 채우는 것입니다
여기- (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 :
는 다음 나는 numberOfSectionsInTableView 메소드를 호출 모든 문자에 대해 다시
- (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;
}
을 ...
제안 사항 그것을 고치는 방법? 나는 아직 내 커피를 없었하지만 어떻게이 심지어 것입니다 내가 볼 수없는
#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
안녕 David, 다음과 같이 코드를 변경했습니다. numberOfRowsInSection : NSDictionary * dictionary = [listOfItems objectAtIndex : section]; NSArray * array = [사전 objectForKey : @ "SCodes"]; return [배열 수]; 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
추측해야한다면 charSpecial은 사전이고 cellForRowAtIndexPath에서는 objectAtIndex를 호출한다고 가정합니다. –
또한 초기화 코드를 확장 할 수 있습니까? 하나의 객체를 사전에 추가하고 해당 사전 만 listOfItems에 추가하는 것으로 보이므로 테이블에는 하나의 객체 만 있습니다. 이 올바른지? –