簡體   English   中英

UITableView滾動時,數據丟失在UITableViewCell上?

[英]Data lose On UITableViewCell When UITableView Scrolling?

我正在嘗試實現基於UITableview的應用程序。 在我的tableView它們是10個部分,每個部分都有一行。 我希望實現每個部分都有不同類型的ContentView (1-8相同的ContentView第9部分不同的ContentView )。 我做了這個代碼。

- (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];

數據會重復。 我怎么能過來這個問題呢?

這一行:

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

永遠不應出現在您的數據源cellForRowAtIndexPath方法中。

除此之外,您的代碼沒問題,除了您沒有在任何地方設置文本字段值。 您需要一個模型(例如10個文本字段值的字符串數組)。 編輯文本字段時應更新此模型,並在上面的方法中將值從模型中復制回文本字段的text屬性:

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

該表以不可預測的方式匯集和重用單元格,以便剛剛從底部滾動的單元格的子視圖可能會重新出現在頂部,或者可能會被丟棄。

這就是你看到它部分工作的原因。 單元格的子視圖工作正常,直到它們的單元格被重用或卸載,然后事物移動到錯誤的位置或數據消失。

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

我建議這樣做:

// 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;
}

我有同樣的問題。 它不是表類的問題。 問題出在您調用此tableviewcontroller的位置。 首先在.h中創建此調用的對象,然后在.m中分配,這就是它..

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

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

看看這個例子,在這個例子中我在tableview單元格中添加圖像視圖,因此你可以添加textview或任何其他像這樣的視圖

UITableViewCell * cell =(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];
  }

所以showin這里imgView =(id)[cell.contentView viewWithTag:55]; 你必須給你標記並在其他地方寫上面顯示的代碼..

讓您嘗試使用以下代碼制作單元格標簽和文本視圖。 這個對我有用。

     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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM