2013-04-09 74 views
2

我已經閱讀了很多關於'dequeueReusableCellWithIdentifier'問題的堆棧,我嘗試了幾個答案,但似乎無法修復它。 我會很感激,如果有人能找到什麼是我的代碼UITableView單元格在滾動時分組搞亂了

部分代碼的問題:

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return 2; 
} 
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 10; 
} 



- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 

{ 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

} 

if (indexPath.section == 0) { 

    if (indexPath.row == 0) { 

     cell.textLabel.text = [arrayOfQuestion objectAtIndex:indexPath.section]; 

    } 

    else if (indexPath.row == 1) { 

     question1 = [[UITextField alloc] initWithFrame: CGRectMake(20, 3, 280, 38) ]; 

     question1.adjustsFontSizeToFitWidth = YES; 

     question1.textColor = [UIColor blackColor]; 

     question1.font = [UIFont systemFontOfSize:17.0]; 

     question1.backgroundColor = [UIColor blueColor]; 

     question1.autocorrectionType = UITextAutocorrectionTypeNo;  // no auto correction support 

     question1.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 

     question1.textAlignment = UITextAlignmentRight; 

     question1.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard) 

     question1.returnKeyType = UIReturnKeyDone; 

     question1.tag = 0; 

//   question1.delegate = self; 



     question1.clearButtonMode = UITextFieldViewModeUnlessEditing; // no clear 'x' button to the right 



     [question1 setEnabled: YES ]; 

     [cell addSubview: question1 ]; 

    } 

} 

else if (indexPath.section == 1) { 

    if (indexPath.row == 0) { 

     cell.textLabel.text = [arrayOfQuestion objectAtIndex:indexPath.section]; 

    } 

    else if (indexPath.row == 1) { 

     question2 = [ [ UITextField alloc ] initWithFrame: CGRectMake(20, 3, 280, 38) ]; 

     question2.adjustsFontSizeToFitWidth = YES; 

     question2.textColor = [UIColor blackColor]; 

     question2.font = [UIFont systemFontOfSize:17.0]; 

     question2.backgroundColor = [UIColor blueColor]; 

     question2.autocorrectionType = UITextAutocorrectionTypeNo;  // no auto correction support 

     question2.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 

     question2.textAlignment = UITextAlignmentRight; 

     question2.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard) 

     question2.returnKeyType = UIReturnKeyDone; 

     question2.tag = 1; 

     //   listTitleTextField.delegate = self; 



     question2.clearButtonMode = UITextFieldViewModeUnlessEditing; // no clear 'x' button to the right 



     [question2 setEnabled: YES ]; 

     [cell addSubview: question2 ]; 

    } 

} 

................ 

return cell; 

} 
+0

我試圖回答這個問題,但我覺得代碼確實需要從頭開始重新考慮。一個簡單的答案是不可能的。我仍然有幾個問題。首先,「搞砸」是什麼意思? – 2013-04-09 20:52:43

+0

當滾動文本字段覆蓋所有單元格時 – RBZ 2013-04-09 20:55:03

回答

1

記住dequeueReusableCellWithIdentifier重複使用相同的對象一遍又一遍地提醒你的細胞。這比在表格視圖中爲未知數量的行創建新單元格更高效。這意味着當你添加UITextFields [cell addSubview: ... ];下一次你需要該單元格時,它已經將該子視圖添加到它的視圖中。

您最好創建一個UITableViewCell的子類,該子類的UITextField已經作爲屬性添加和訪問。然後你可以有兩個單元格標識符:一個是指基本的問題UITableViewCell和一個引用答案的問題,這是你的新子類。

最重要的是,我會考慮通過重構構建表格單元格的方式來使代碼更加靈活。因爲它不是非常具有伸縮性,所以我敢打賭,這是一個試圖獲得答案的噩夢。

相關問題