2010-05-01 35 views
1

请注意我在哪里有NSLOG。所有它在日志中显示的是nameSection中的前三个项目。经过一些测试后,我发现它显示了有多少个键,因为如果我向plist添加一个键,它会在日志中记录第四个项目。iphone奇怪的问题,当使用自定义单元

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

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

所以如果我一起工作的字典具有3个阵列,的NSLog将从第一阵列显示3个字符串:

2010-05-01 17:03:26.957清单[63926:207] string0 2010- 05-01 17:03:26.960检查列表[63926:207] string1 2010-05-01 17:03:26.962检查列表[63926:207] string2

然后停下来:2010-05-01 17:03: 26.963检查列表[63926:207] *由于未捕获异常'NSRangeException'而终止应用,原因:'* - [NSCFArray objectAtIndex:]:index(3)beyond bounds(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; 
} 
+0

请问您可以减少代码到相关部分吗? – Cesar 2010-05-02 01:18:18

+0

我做到了。我不知道代表在哪里可能会搞砸 – Brodie 2010-05-02 03:41:38

回答

0

发现问题。这是(无意义的)代码行。删除它,它工作正常。

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

0

在的tableView:的cellForRowAtIndexPath:你这样做:

​​

这具有至少两个问题:

  1. 根据您对numberOfSectionsInTableView的实现:,看起来您每节只有一个键。此代码正在为当前indexPath.section中的每一行查找关键字。

  2. 在tableView:numberOfRowsInSection: - [keys objectAtIndex:]中返回一个NSString。这里你将它视为一个NSDictionary。