我在堆栈上读了很多关于'dequeueReusableCellWithIdentifier'的问题,我尝试了几个答案,我似乎无法修复它。 如果有人能找到我的代码中的问题,我将不胜感激
部分代码:
- (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 :(得分:1)
记住dequeueReusableCellWithIdentifier
一遍又一遍地重复使用同一个对象来绘制单元格。它比在表视图中为未知行数创建新单元格更有效。这意味着当您下次需要该单元格时添加带有[cell addSubview: ... ];
的UITextFields时,它已经将该子视图添加到其视图中。
您最好创建UITableViewCell
的子类,其中UITextField
已添加并可作为属性访问。然后你可以有两个单元格标识符:一个引用基本UITableViewCell
的问题,另一个引用答案,这是你的新子类。
最重要的是,我会通过重构你构建表格单元格的方式来提高代码的灵活性。因为它不是很可扩展,我打赌一个噩梦试图访问答案。