我有一個簡單的tableView有20行。我創建了一個子類自定義的UITableview單元格,並在cellforRowAtIndex中,我每隔一行添加一個文本字段到該單元格一行。當我滾動上下文本字段出現在錯誤的行。請注意,我無法使UItextfield成爲我自定義單元格的一部分,因爲這可以是任何內容,複選框,單選按鈕,但爲了簡單起見,我選擇了UITextfield ...我做錯了什麼?UITableViewCell可重用性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TestCellIdentifier";
testCell *cell = (testCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
else{
//HMMM I also tried removing it before adding it- it doesn't work neither
for(UIView *v in cell.subviews){
if(v.tag == 999){
[v removeFromSuperview];
}
}
//add UItextField to row if it's divisible by 3
if(indexPath.row %3 ==0){
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(400, 10, 300, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = [NSString stringWithFormat:@"%d",indexPath.row];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.tag = 999;
[cell addSubview:textField];
}
}
cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;
}
是的,我是有工作沒有可重複使用的電池,我只是擔心,它是沒有效率的,但我的表不會超過25排,所以我認爲我可能會很好。謝謝 – IronMan1980