2011-07-01 31 views
1

我有一個UITableView一些UITextField上表視圖單元格。 UITextField s在用戶輸入文本字段的一些值,然後滾動表視圖。文本字段上的值不會持續。下面的代碼滾動文本字段值表格視圖不會持續

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return 100; 
} 

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

static NSString *CellIdentifier = @"Cell"; 
static NSString *CellIdentifierFirst = @"CellFirst"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierFirst]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; 

} 

NSArray *cellSubs = cell.contentView.subviews; 
for (int i = 0 ; i < [cellSubs count] ; i++) { 
    [[cellSubs objectAtIndex:i] removeFromSuperview]; 
} 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

UITextField * textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 150, 40)]; 
textFieldRounded.borderStyle = UITextBorderStyleNone; 
textFieldRounded.textColor = [UIColor blackColor]; 
textFieldRounded.font = [UIFont systemFontOfSize:17.0]; 
textFieldRounded.placeholder = @"Type here"; 
textFieldRounded.backgroundColor = [UIColor whiteColor]; 
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; 
textFieldRounded.keyboardType = UIKeyboardTypeDefault; 
textFieldRounded.returnKeyType = UIReturnKeyDone; 
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; 
textFieldRounded.delegate = self; 
[cell.contentView addSubview:textFieldRounded]; 
return cell; 
} 

所以請大家幫幫我大家

+0

你在哪裏添加文本字段? –

回答

0

你需要保持一個可變的數組中的所有行。每次更改文本時,都必須將該文本存儲在該數組中,並在調用cellForRowAtIndexPath:時設置數組中的文本。

3

我在我的一個應用程序中有相同的功能,我使用下面的代碼來實現這一點,我從來沒有這樣的問題。

首先,您需要將所有textField值臨時存儲在Array中。像這樣做數組。

arrTemp=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@""], 
    [NSString stringWithFormat:@""], 
    [NSString stringWithFormat:@""], 
    [NSString stringWithFormat:@""], 
    [NSString stringWithFormat:@""], 
    [NSString stringWithFormat:@""], 
    [NSString stringWithFormat:@""], 
    [NSString stringWithFormat:@""], 
    [NSString stringWithFormat:@""],nil]; 

Then Give all textField tag = indexPath.row;

之後您需要在以下兩種方法中替換textField值。

-(BOOL)textFieldShouldReturn:(UITextField *)textField{ 
    [arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text]; 
} 

-(void)textFieldDidEndEditing:(UITextField *)textField{ 
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text]; 
} 

最後,你需要設置的cellForRowAtIndexPath數據源方法,它的價值。因此,無論何時滾動tableview,它都會從temp數組中設置以前的值。喜歡這個。

cell.txtEntry.text = [arrTemp objectAtIndex:indexPath.row]; 

它可能我忘了一些代碼粘貼在這裏。所以,如果你有任何問題,請讓我知道。

相關問題