2010-05-01 34 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。