2012-03-23 64 views
2

我正在嘗試實施基於UITableview的應用程序。在我的tableView他們是10節,每節有一行。 我要實現的每個部分都有不同類型的ContentView(1-8個相同ContentView第9個不同的ContentView)。我爲此做了這個代碼。數據丟失在UITableViewCell上時UITableView滾動?

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return 1; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier1 = @"Cell1"; 
    static NSString *CellIdentifier2 = @"Cell2"; 
    UITextField *textField; 
    UITextView *textView; 
    NSUInteger section=[indexPath section]; 
    if(section == 9){ 
     UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath]; 
     //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if(cell==nil){ 
      cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease]; 
      textView=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)]; 
      [textView setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor 
              ]]; 
      [textView setTag:([indexPath section]+100)]; 
      [cell.contentView addSubview:textView]; 
     }else{ 
      textView=(UITextView*)[cell.contentView viewWithTag:([indexPath section]+100)]; 
     } 
     return cell; 
    }else { 
     UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath]; 
     // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
     if(cell==nil){ 
      cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease]; 
      textField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)]; 
      [textField setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]]; 
      [textField setTag:([indexPath section]+100)]; 
      [cell.contentView addSubview:textField]; 
     }else{ 
      textField=(UITextField*)[cell.contentView viewWithTag:([indexPath section]+100)]; 

     } 

     return cell; 
    } 


    return nil; 

} 

我的問題是:1。 類型後在UITextField/UITextView有些事情我在UITableView滾動。那段時間UITableViewCellUITextField/UITextView)中的所有數據都丟失了,除了最後一個單元格數據。 2.如果我創建細胞

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

代替

UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath]; 

的數據將重複。我怎麼能過來這個問題?

回答

9

這條線:

UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath]; 

不應該出現在您的數據源的cellForRowAtIndexPath方法。

除此之外,你的代碼是確定的,除了你不在任何地方設置文本字段值。您需要一個模型(例如10個文本字段值的字符串數組)。你上面複製的價值背出模型,進入文本框的文本屬性時,文本框進行編輯這個模型應該更新,並在你的方法:

textfield.text = [self.modelArray objectAtIndex:indexPath.section]; 
+0

用戶將輸入數據/文本到UITextField – John 2012-03-23 08:41:18

+1

實際上發生的是什麼意思,輸入數據/文本到UITextField/UITextView我滾動uitableview。那時某些細胞會消失,如果再次向相反的方向滾動細胞將會出現,那麼我以前輸入的那個,就是輸了。數據持久性是主要問題。 – John 2012-03-23 08:50:17

+0

我應該寫這個UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath]; – John 2012-03-23 08:54:25

0

嘿原因是你正在做這件事時,細胞是零?但是當單元格不是零時,你不寫任何代碼。

看看這個例子,在這個例子中我在tableview中小區追加圖像視圖中,因此可以添加textviews或類似的任何其它視圖此

的UITableViewCell *細胞=(的UITableViewCell *)[的tableView dequeueReusableCellWithIdentifier:CellIdentifier] ;

UIImageView *imgView; 
    if(cell == nil) 
    { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero    reuseIdentifier:CellIdentifier] autorelease]; 

    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,100,62)]; 
    [imgView setImage:[UIImage imageNamed:@"img.png"]]; 
    imgView.tag = 55; 
    [cell.contentView addSubview:imgView]; 
    [imgView release]; 
} 
else 
{ 
    imgView = (id)[cell.contentView viewWithTag:55]; 
    } 

以便舒這裏imgView =(ID)[cell.contentView viewWithTag:55];你必須給你的標籤,並在其他地方寫上面的代碼。

+0

我認爲這是不正確的。因爲如果我實現其他條件,那麼也會出現同樣的問題。 – John 2012-03-23 06:57:06

+0

不,這是正確的。只是執行並檢查它。只需檢查我的代碼,然後添加您的textviews,您的代碼按照我的代碼,它肯定會工作.. – PJR 2012-03-23 06:59:15

+0

如果不介意我會給我的代碼,你可以檢查? – John 2012-03-23 07:01:20

0

讓你嘗試使用下面的代碼來製作單元格標籤和文字瀏覽。這個對我有用。

 if (tagvalue ==3) { 

     static NSString *CellIdentifier = @"Cell3"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
     reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     lbl7 = [[UILabel alloc] init]; 
     [lbl7 setFont:[UIFont boldSystemFontOfSize:20]]; 
     [cell.contentView addSubview:lbl7]; 
     lbl7.backgroundColor = [UIColor clearColor]; 
     lbl7.frame = CGRectMake(120, 5, 0, 40); 
     lbl7.tag = 70; 
     [lbl7 release]; 

     lbl8 = [[UILabel alloc] init]; 
     [lbl8 setFont:[UIFont boldSystemFontOfSize:18]]; 
     [cell.contentView addSubview:lbl8]; 
     lbl8.backgroundColor = [UIColor clearColor]; 
     lbl8.textColor = [UIColor grayColor]; 
     lbl8.frame = CGRectMake(120, 50, 0, 40); 
     lbl8.tag = 80; 
     [lbl8 release]; 

     lbl7 = (UILabel*)[cell.contentView viewWithTag:70]; 
     lbl8 = (UILabel*)[cell.contentView viewWithTag:80]; 
     lbl7.text = [[rowsarray objectAtIndex:row]objectForKey:@"name"]; 
     lbl8.text = [[rowsarray objectAtIndex:row]objectForKey:@"flavour"]; 
     [lbl7 sizeToFit]; 
     [lbl8 sizeToFit]; 

     return cell; 
    } 
    if (tagvalue ==4) { 
     static NSString *CellIdentifier = @"Cell4"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     lbl9 = [[UILabel alloc] init]; 
     [lbl9 setFont:[UIFont boldSystemFontOfSize:20]]; 
     [cell.contentView addSubview:lbl9]; 
     lbl9.backgroundColor = [UIColor clearColor]; 
     lbl9.frame = CGRectMake(120, 5, 0, 40); 
     lbl9.tag = 90; 
     [lbl9 release]; 

     lbl10 = [[UILabel alloc] init]; 
     [lbl10 setFont:[UIFont boldSystemFontOfSize:18]]; 
     [cell.contentView addSubview:lbl10]; 
     lbl10.backgroundColor = [UIColor clearColor]; 
     lbl10.textColor = [UIColor grayColor]; 
     lbl10.frame = CGRectMake(120, 50, 0, 40); 
     lbl10.tag = 100; 
     [lbl10 release];    
     lbl9 = (UILabel*)[cell.contentView viewWithTag:90]; 
     lbl10 = (UILabel*)[cell.contentView viewWithTag:100]; 
     lbl9.text = [[rowsarray objectAtIndex:row]objectForKey:@"name"]; 
     lbl10.text = [[rowsarray objectAtIndex:row]objectForKey:@"flavour"]; 
     [lbl9 sizeToFit]; 
     [lbl10 sizeToFit]; 

     return cell; 
    } 
1

表池和重用細胞在不可預知的方式,因此剛剛從底部滾動的單元格的子視圖可能會重新出現在頂部,或者可能會被丟棄。

這就是爲什麼你看到它部分工作。單元格的子視圖工作正常,直到它們的單元格被重用或卸載,然後事件移動到錯誤的地方或數據消失。

解決方案是您的表的數據源需要保存它自己的數據。這通常是代表你的模型的數組。你的情況有點不尋常,因爲你使用表格中的文本控件作爲輸入,而不是顯示,這是比較典型的。

我建議做這樣的:

// in @interface 
@property (nonatomic, retain) NSMutableArray *sections; 

// in @implementation 
@synthesize sections=_sections; 

// at some point before the view appears 
self.sections = [NSMutableArray array]; 
for (int i=0; i<10; i++) { 
    UIControl *textControl; 
    if (i<9) { 
     textControl=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)]; 
    } else { 
     textControl=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)]; 
    } 
    [textControl setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]]; 
    [textControl setTag:i+100]; 
    [sections addObject:textControl]; 
    [textControl release]; 
} 

現在你的cellForRowAtIndexPath有點簡單:

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

    static NSString *CellIdentifier1 = @"Cell1"; 
    static NSString *CellIdentifier2 = @"Cell2"; 

    NSUInteger section=[indexPath section]; 

    if(section == 9) { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if(cell==nil) { 
      cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease]; 
     } 

    } else { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
     if(cell==nil) { 
      cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease]; 
     } 
    } 

    // who knows what subview this cell has? it might not have one, or it might have the wrong one 
    // just clean it up to be certain 
    for (UIView *view in cell.subviews) { 
     [view removeFromSuperView]; 
    } 

    // get the textControl we set up for _this_ section/cell 
    UIControl *textControl = [self.sections objectAtIndex:section]; 

    // now we have a fresh cell and the right textControl. drop it in 
    [cell addSubview:textControl]; 
    return cell; 
} 
+0

非常感謝.... – John 2012-03-23 10:27:36

0

我有同樣的問題。它不是表類的問題。問題出在你調用這個tableviewcontroller的地方。首先讓這個呼叫的對象在.h中,然後在.m中分配,那就是它..

當我在像tbl *t = [self.storyboard...];這樣的viewdidload中聲明時,我也遇到了同樣的問題。但是當我把tbl *t;的.h問題解決了。