2011-10-28 27 views
0

我向桌面視圖添加了幾個單元格,每個單元格右邊有一個textField讓用戶輸入文本。我也爲此添加了一個自定義單元類。我發現,當我向下滾動並返回時,前幾行的輸入將消失。有人知道有什麼問題嗎?iPhone上的表格視圖滾動文本字段設置爲零

這裏有一塊我的代碼

@implementation MyCell 
-(void)setData:(NSString*)str 
{ 

    UIImage *image = [UIImage imageNamed:@"cellImage copy.png"]; 
    [[self imageView ]setImage:image]; 

    lblLocations=[[UILabel alloc]init]; 
    lblLocations.textAlignment=UITextAlignmentLeft; 
    lblLocations.backgroundColor = [UIColor clearColor]; 
    lblLocations.font = [UIFont boldSystemFontOfSize:12]; 
    lblLocations.numberOfLines = 2; 
    lblLocations.lineBreakMode = UILineBreakModeWordWrap; 
    lblLocations.textAlignment = UITextAlignmentCenter; 
    lblLocations.text =str ; 
    NSLog(@"the title is%@",str); 
    [[self contentView]addSubview:lblLocations]; 
    [lblLocations release]; 

    txtUnits=[[UITextField alloc]init]; 
    [txtUnits setAdjustsFontSizeToFitWidth:YES]; 
    txtUnits.textAlignment = UITextAlignmentCenter; 

    txtUnits.returnKeyType = UIReturnKeyDone; 
    txtUnits.autocapitalizationType = NO; 
    txtUnits.textColor = [UIColor blackColor]; 
    //following condition is not working when i'm scrolling up after writing into some of cell's text fields or i can say never working at all 
    if ([textDict valueForKey:[NSString stringWithFormat:@"%d",rowNum]]) { 
    [txtUnits setText:[textDict valueForKey:[NSString stringWithFormat:@"%d",rowNum]]]; 
    } 
    else 
    { 
    [txtUnits setPlaceholder:@"0.00"]; 
    } 
    [txtUnits setValue:[UIColor blackColor] 
      forKeyPath:@"_placeholderLabel.textColor"]; 
    [[self contentView]addSubview:txtUnits]; 
    txtUnits.backgroundColor = [UIColor clearColor]; 
    txtUnits.delegate = self; 
    [txtUnits release]; 
} 
-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [textDict setObject:textField.text forKey:[NSString stringWithFormat:@"%d",rowNum]]; 
} 
and cellforrow.... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell=nil; 
    if (cell == nil) { 
     cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier androw:[indexPath row]] autorelease]; 
    } 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    NSMutableDictionary *dict=(NSMutableDictionary*)[locations objectAtIndex:[indexPath section]]; 

    NSMutableArray *array=(NSMutableArray*)[dict objectForKey:@"locationsArray"]; 

    MyCell *modelObj=(MyCell*)[array objectAtIndex:indexPath.row]; 

    NSLog(@"the values of the array are%@",modelObj.title); 
    NSString *str = modelObj.title; 
    [(MyCell *)cell setData:str]; 
return cell; 

}
應該是什麼邏輯檢查文本是否被輸入到一個特定的單元格的文本字段?請幫忙!

回答

0

嗯 - 爲什麼在每次調用cellForRowAtIndexPath時重新分配MyCell?在我看來,你應該更喜歡這樣的:

static NSString *CellIdentifier = @"Cell"; 

MyCell* cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (!cell) { 
... 
} 

此外,它似乎很奇怪,你正在使用您的視圖類爲模型類,以及 - 被如何維護輔助locations字典?如果實際上模型實例與視圖實例相同,爲什麼重複?

相關問題