开发者

iphone Odd Problem when using a custom cell

开发者 https://www.devze.com 2022-12-29 08:49 出处:网络
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

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.

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

the plist file has 3 dictionaries, each with se开发者_如何转开发veral 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.

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

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

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)'

if i added an array to the dictionary, it log 4 items instead of 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;
}


In tableView:cellForRowAtIndexPath: you're doing this:

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.

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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消