iphone在使用自定义手机时出现奇怪的问题

iphone在使用自定义手机时出现奇怪的问题,iphone,uitableview,ipad,cell,Iphone,Uitableview,Ipad,Cell,请注意我在哪里有NSLOG。它在日志中显示的只是名称部分的前三项。经过一些测试,我发现它显示了有多少个键,因为如果我向plist添加一个键,它将在日志中记录第四项 nameSection应该是组成plist文件中键数组的字符串数组 plist文件有3个字典,每个字典都有几个字符串数组。代码正确地选择了我正在使用的字典,然后应该使用数组名称作为表中的部分,每个数组中的字符串作为每个单元格中要显示的内容 因此,如果我正在使用的字典有3个数组,NSLOG将显示第一个数组中的3个字符串: 2010-05

请注意我在哪里有NSLOG。它在日志中显示的只是名称部分的前三项。经过一些测试,我发现它显示了有多少个键,因为如果我向plist添加一个键,它将在日志中记录第四项

nameSection应该是组成plist文件中键数组的字符串数组

plist文件有3个字典,每个字典都有几个字符串数组。代码正确地选择了我正在使用的字典,然后应该使用数组名称作为表中的部分,每个数组中的字符串作为每个单元格中要显示的内容

因此,如果我正在使用的字典有3个数组,NSLOG将显示第一个数组中的3个字符串:

2010-05-01 17:03:26.957检查表[63926:207]0 2010-05-01 17:03:26.960检查表[63926:207]string1 2010-05-01 17:03:26.962检查表

然后停止:2010-05-01 17:03:26.963检查表[63926:207]*由于未捕获的异常“NSRangeException”终止应用程序,原因:“*-[NSCFArray objectAtIndex:::]:索引(3)超出边界(3)”

如果我在字典中添加了一个数组,它会记录4项而不是3项

我希望这个解释有道理

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [keys count];
}

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger) section {
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    return [nameSection count];

}

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

    NSUInteger section = [indexPath section];
    NSString *key = [keys objectAtIndex: section];
    NSArray *nameSection = [names objectForKey:key];
    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    static NSString *ChecklistCellIdentifier = @"ChecklistCellIdentifier "; 

    ChecklistCell *cell = (ChecklistCell *)[tableView 
                                            dequeueReusableCellWithIdentifier: SectionsTableIdentifier];
 if (cell == nil)  
 {
 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChecklistCell" 
 owner:self options:nil];
 for (id oneObject in nib)
 if ([oneObject isKindOfClass:[ChecklistCell class]])

cell = (ChecklistCell *)oneObject;
 }
 NSUInteger row = [indexPath row];
 NSDictionary *rowData = [self.keys objectAtIndex:row];

 NSString *tempString = [[NSString alloc]initWithFormat:@"%@",[nameSection objectAtIndex:row]];

NSLog(@"%@",tempString);  
cell.colorLabel.text = [tempArray objectAtIndex:0];
 cell.nameLabel.text = [tempArray objectAtIndex:1];
 return cell;


return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{
    NSString *key = [keys objectAtIndex:section];
    return key;
}

在tableView:cellForRowAtIndexPath:中,您正在执行以下操作:

NSDictionary *rowData = [self.keys objectAtIndex:row];
这至少有两个问题:

  • 根据NumberOfSectionsTableView:的实现,似乎每个节都有一个键。这段代码正在为当前indexPath.section中的每一行寻找一个键

  • 在tableView:numberOfRowsInSection:,-[keys objectAtIndex:]中返回一个NSString。在这里,你把它当作一本词典


  • 发现了问题。这是一行(毫无意义的)代码。把它取下来,效果很好


    NSDictionary*rowData=[self.keys objectAtIndex:row]

    你能把代码简化成相关的部分吗?我做了。我不知道代表们哪里会搞砸