나는 20 행의 간단한 tableView를 가지고 있습니다. 하위 클래스 사용자 정의 UITableview 셀을 만들고 cellforRowAtIndex에서 다른 모든 3 행에 한 번 셀에 텍스트 필드를 추가합니다. 위아래로 스크롤 할 때 텍스트 필드가 잘못된 행에 표시됩니다. 참고,이 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