使用自定义单元格时出现iphone奇怪问题

时间:2010-05-01 23:19:03

标签: iphone uitableview ipad cell

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

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)超出边界(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;
}

2 个答案:

答案 0 :(得分:0)

在tableView:cellForRowAtIndexPath中:你这样做:

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

这至少有两个问题:

  1. 根据您对numberOfSectionsInTableView:的实现,您似乎每个部分都有一个密钥。此代码正在查找当前indexPath.section中每一行的键。

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

答案 1 :(得分:0)

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

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