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

时间:2022-09-04 12:37:55

Please note where I have the NSLOG. All it is displaying in the log is the first three items in the nameSection. After some testing, I discovered it is displaying how many keys there are because if I add a key to the plist, it will log a fourth item in log.

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

nameSection should be an array of the strings that make up the key array in the plist file.

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

the plist file has 3 dictionaries, each with several arrays of strings. The code picks the dictionary I am working with correctly, then should use the array names as sections in the table and the strings en each array as what to display in each cell.

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

so if the dictionary i am working with has 3 arrays, NSLOG will display 3 strings from the first array:

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

2010-05-01 17:03:26.957 Checklists[63926:207] string0 2010-05-01 17:03:26.960 Checklists[63926:207] string1 2010-05-01 17:03:26.962 Checklists[63926:207] string2

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]字符串2

then stop with: 2010-05-01 17:03:26.963 Checklists[63926:207] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (3) beyond bounds (3)'

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

if i added an array to the dictionary, it log 4 items instead of 3.

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

I hope this explanation makes sense...

我希望这个解释有意义......

-(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 个解决方案

#1


0  

In tableView:cellForRowAtIndexPath: you're doing this:

在tableView:cellForRowAtIndexPath中:你这样做:

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

This has at least two problems:

这至少有两个问题:

  1. Based on your implementation of numberOfSectionsInTableView:, it seems you have one key per section. This code is looking for a key for every row in the current indexPath.section.

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

  2. In tableView:numberOfRowsInSection:, -[keys objectAtIndex:] returns an NSString. Here you're treating it as an NSDictionary.

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

#2


0  

Found the problem. It was this (pointless) line of code. Removed it and it worked fine.

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

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

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

#1


0  

In tableView:cellForRowAtIndexPath: you're doing this:

在tableView:cellForRowAtIndexPath中:你这样做:

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

This has at least two problems:

这至少有两个问题:

  1. Based on your implementation of numberOfSectionsInTableView:, it seems you have one key per section. This code is looking for a key for every row in the current indexPath.section.

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

  2. In tableView:numberOfRowsInSection:, -[keys objectAtIndex:] returns an NSString. Here you're treating it as an NSDictionary.

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

#2


0  

Found the problem. It was this (pointless) line of code. Removed it and it worked fine.

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

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

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