-1
이 질문에 이미 많은 답변이 있지만 내 문제의 해결책이 없습니다.UITableViewCell의 동적 텍스트 필드에서 스크롤 할 때 데이터가 손실됩니다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *CellIdentifier = @"Cell";//[NSString st ringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *textField_Name, *textField_PhoneNumber;
UIButton *button_AddToMyCon;
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundColor:[UIColor clearColor]];
if (indexPath.row < rowcount-2) {
textField_Name = [[UITextField alloc]init];
textField_PhoneNumber = [[UITextField alloc]init];
textField_Name.frame = CGRectMake(10, (cell.frame.size.height-20)/2, 143, 30);
textField_PhoneNumber.frame = CGRectMake(167, (cell.frame.size.height-20)/2, 143, 30);
textField_Name.delegate = self;
textField_PhoneNumber.delegate = self;
textField_Name.borderStyle = UITextBorderStyleRoundedRect;
textField_PhoneNumber.borderStyle = UITextBorderStyleRoundedRect;
textField_Name.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Link Name" attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
textField_PhoneNumber.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Web Link/URL" attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
textField_Name.layer.cornerRadius = 5.0;
textField_Name.clipsToBounds = YES;
[textField_Name.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[textField_Name.layer setBorderWidth:0.5];
textField_Name.font = [UIFont fontWithName:@"arial" size:15.0];
textField_PhoneNumber.layer.cornerRadius = 5.0;
textField_PhoneNumber.clipsToBounds = YES;
[textField_PhoneNumber.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[textField_PhoneNumber.layer setBorderWidth:0.5];
textField_PhoneNumber.font = [UIFont fontWithName:@"arial" size:15.0];
textField_Name.textColor = [UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:1.0f];
textField_PhoneNumber.textColor = [UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:1.0f];
// textField_Name.tag=501+indexPath.row;
textField_Name.tag = ktagName;
NSLog(@"textField_Name.tag %ld",(long)textField_Name.tag);
//textField_PhoneNumber.tag=701+indexPath.row;
textField_PhoneNumber.tag =ktagPhn;
NSLog(@"textField_PhoneNumber.tag %ld",(long)textField_PhoneNumber.tag);
if(intEditSave == 1)
{
[textField_Name setUserInteractionEnabled:NO];
[textField_PhoneNumber setUserInteractionEnabled:NO];
}
else
{
[textField_Name setUserInteractionEnabled:YES];
[textField_PhoneNumber setUserInteractionEnabled:YES];
}
textField_PhoneNumber.keyboardType = UIKeyboardTypeURL;
[cell addSubview:textField_Name];
[cell addSubview:textField_PhoneNumber];
UILabel *linelable=[[UILabel alloc]initWithFrame:CGRectMake(0, cell.contentView.frame.size.height+10, cell.contentView.frame.size.width, 0.5)];
[cell.contentView addSubview:linelable];
[linelable setBackgroundColor:[UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:0.2f]];
}
else if (indexPath.row==rowcount-2){
NSLog(@"indexPath.row==rowcount-1");
UIButton* button_AddMore = [[UIButton alloc]init];
[button_AddMore setTitle:ktagAdd_More forState:UIControlStateNormal];
[button_AddMore titleLabel].font = [UIFont fontWithName:@"arial" size:15.0];
[button_AddMore setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button_AddMore addTarget:self action:@selector(addMoreButton:) forControlEvents:UIControlEventTouchUpInside];
// button_AddMore.tag = ktagbutton_AddMore;
button_AddMore.frame = CGRectMake(90, (cell.frame.size.height-20)/2, 140, 30);
[button_AddMore setBackgroundImage:[UIImage imageNamed:@"addMore_button.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:button_AddMore];
button_AddMore.tag=indexPath.row;
}
else if (indexPath.row==rowcount-1)
{
button_AddToMyCon = [[UIButton alloc]init];
[button_AddToMyCon setTitle:ktagAdd_to_MyCon forState:UIControlStateNormal];
[button_AddToMyCon titleLabel].font = [UIFont fontWithName:@"arial" size:15.0];
[button_AddToMyCon setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button_AddToMyCon addTarget:self action:@selector(save_Fields:) forControlEvents:UIControlEventTouchUpInside];
button_AddToMyCon.tag = ktagbutton_AddToMyCon;
button_AddToMyCon.frame = CGRectMake(90, (cell.frame.size.height-20)/2, 140, 30);
[button_AddToMyCon setBackgroundColor:[UIColor colorWithRed:(234.0f/255.0f) green:(134.0f/255.0f) blue:(59.0f/255.0f) alpha:1.0]];
button_AddToMyCon.layer.cornerRadius=4;
[cell.contentView addSubview:button_AddToMyCon];
UILabel *linelable=[[UILabel alloc]initWithFrame:CGRectMake(0, cell.contentView.frame.size.height+10, cell.contentView.frame.size.width, 0.5)];
[linelable setBackgroundColor:[UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:0.2f]];
}
}
return cell;
}
-(void)addMoreButton:(UIButton *)sender
{
rowcount=rowcount+1;
int section = 0;
long row = rowcount;
NSIndexPath* path = [NSIndexPath indexPathForRow:row-3 inSection:section];
[(UITableView *)[self.view viewWithTag:ktagtableView_detailsEdit] insertRowsAtIndexPaths:[NSArray arrayWithObjects:path, nil] withRowAnimation:UITableViewRowAnimationLeft];
NSIndexPath* top = [NSIndexPath indexPathForRow:rowcount-1 inSection:0];
[(UITableView*)[self.view viewWithTag:ktagtableView_detailsEdit] scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionBottom animated:YES]; //
//[(UITableView *)[self.view viewWithTag:ktagtableView_detailsEdit]reloadData];
}
여기서 시나리오는 tableview에 각각 2 개의 텍스트 필드가 포함 된 4 개의 행 기본값이있는 시나리오입니다. 추가 버튼과 저장 버튼 하나가 추가되었습니다. 더 많은 버튼을 클릭하면 매번 한 행이 추가됩니다. 위의 사항을 알려 주시기 바랍니다.
당신이 외부에서 코드를 삽입하려고 않은 경우 (셀 == 전무) {세포 = [[있는 UITableViewCell ALLOC] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : CellIdentifier] } ... 여기 ... –
행을 삽입하는 동안 beginUpdate 및 endUpdate 메소드를 호출 할 수 있습니다. 또한 테이블을 다시로드하여 손실되는 데이터를 다시 가져올 수도 있습니다. – NewStackUser
알다시피, 아마도'cell'이 값없이 참조되고 있다고 말하는 컴파일러 경고가있었습니다. –