2013-06-13 36 views
-1

我有一個tableview包含30個字段(標籤,文本框,星級和其他一些UIView,picercontrol)。問題是,當我滾動表格視圖時,單元格被替換。任何解決方案? 編輯:Tableview在滾動過程中的奇怪行爲

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ReuseCell"; 
    // UIColor* mainColor = [UIColor colorWithRed:43.0/255 green:128.0/255 blue:191.0/255 alpha:1.0f]; 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell==nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; 
     if (indexPath.section==0) { 

      if (indexPath.row==0) { 
       productImage=[self createImageWithFrame: CGRectMake(115.5, 10, 89,89)]; 
       productImage.tag=indexPath.row+indexPath.section; 
       [cell.contentView addSubview: productImage]; 
      } 
      else{ 
       titleLabel1=[self createLabelWithFrame:CGRectMake(4, 5, 100, 30)];////normal label 
       titleLabel1.tag=indexPath.row+indexPath.section; 
       [cell.contentView addSubview:titleLabel1]; 
       titleLabel1.text=[labelArray objectAtIndex:indexPath.row-1]; 
       if (indexPath.row==2) { 
       productImage=[self createImageWithFrame: CGRectMake(104, 10, 190,20)]; 
       productImage.tag=indexPath.row+indexPath.section;; 
       [cell.contentView addSubview: productImage]; 

       } 
       else if (indexPath.row==7) { 
       titleLabel1=[self createLabelWithFrame:CGRectMake(4, 5, 100, 30)];////time label 
       titleLabel1.tag=indexPath.row+indexPath.section; 
       [cell.contentView addSubview:titleLabel1]; 
       } 
       else if (indexPath.row==8) { 
       titleLabel1=[self createLabelWithFrame:CGRectMake(4, 5, 100, 30)];///time label 
       titleLabel1.tag=indexPath.row+indexPath.section; 
       [cell.contentView addSubview:titleLabel1]; 
       } 
       else if (indexPath.row==9) { 
        descriptionText=[[UITextView alloc]initWithFrame:CGRectMake(104, 10, 190, 50)]; 
        descriptionText.tag =indexPath.row+indexPath.section; 
        [cell.contentView addSubview:descriptionText]; 
       } 
       else{ 
       textFld=[self createTextFieldWithFrame:CGRectMake(104, 10, 190, 20)]; 
       textFld.tag=indexPath.row+indexPath.section; 
       [cell.contentView addSubview:textFld]; 
        textFld.text=[labelArray objectAtIndex:indexPath.row-1]; 
       } 
      } 

     } 
    } 

    ///////trying to resuse the cell 
    if (indexPath.section==0) { 
     if (indexPath.row==0) { 
      productImage=(UIImageView *)[cell.contentView viewWithTag:indexPath.row+indexPath.section]; 
      //productImage.image= 
     } 
     else{ 
      titleLabel1=(UILabel *)[cell.contentView viewWithTag:indexPath.row+indexPath.section]; 
      titleLabel1.text=[labelArray objectAtIndex:indexPath.row+indexPath.section-1]; 
      if (indexPath.row==2) { 
       // productImage=[self createImageWithFrame: CGRectMake(104, 10, 190,20)]; 


      } 
      if (indexPath.row==7) { 
       // titleLabel1=(UILabel *)[cell.contentView viewWithTag:indexPath.row+indexPath.section];////time label 


      } 
      if (indexPath.row==8) { 
       // titleLabel1=(UILabel *)[cell.contentView viewWithTag:indexPath.row+indexPath.section];;///time label 


      } 
     // textFld=(UITextField *)[cell.contentView viewWithTag:indexPath.row+indexPath.section]; 

      if (indexPath.row==9) { 

      } 
     } 

    } 

編輯:離開7節1節:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ReuseCell"; 
    // UIColor* mainColor = [UIColor colorWithRed:43.0/255 green:128.0/255 blue:191.0/255 alpha:1.0f]; 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell==nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; 
     cell.tag=indexPath.row; 
     if (indexPath.row!=0) { 
      titleLabel1=[self createLabelWithFrame:CGRectMake(4, 5, 100, 30)]; 
      titleLabel1.tag=indexPath.row+indexPath.section; 
      [cell.contentView addSubview:titleLabel1]; 

     } 
     NSLog(@"%d",indexPath.row); 
    } 
    else{ 
     titleLabel1=(UILabel *)[cell.contentView viewWithTag:indexPath.row+indexPath.section]; 
    } 
    if (indexPath.row!=0) 
     titleLabel1.text=[labelArray objectAtIndex:indexPath.row+indexPath.section-1]; 
     return cell; 
} 
and my labelArray is: 
labelArray=[[NSMutableArray alloc]initWithObjects:@"*Name",@"Category",@"Brand",@"Model",@"Serial no",@"Bill no",@"*Purchase date",@"*Expiry Date",@"Description",nil]; 
+0

多少節你有在UITableView中的 –

+0

可能重複[UITableViewCell的重複,當滾動](http://stackoverflow.com/questions/17063022/uitableviewcell-repeating-when-scroll) - 永遠不要重複你自己的問題。 – hakre

回答

1

問題出在正確標記單元格內的視圖。在您的實現中,第一部分的第一行和第一部分的第零行將使用相同的數字進行標記。

你應該標記通過以下方式將意見:

productImage=(UIImageView *)[cell.contentView viewWithTag:indexPath.row+indexPath.section*1000]; 
0

當您滾動您的表視圖則對應電池在內部調用重載方法。爲避免出現這種情況,您應該使用可重複使用的單元格:

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

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 
+0

但問題是如何檢索單元格內容 – user2211689

2

的問題是在設置變量值: 使用indexPath.row*indexPath.section+9999(some value);

嘗試

productImage.tag=indexPath.row+indexPath.section*1000; 

通過同樣的方式檢索它們此:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ReuseCell"; 
    // UIColor* mainColor = [UIColor colorWithRed:43.0/255 green:128.0/255 blue:191.0/255 alpha:1.0f]; 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell==nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; 
     if (indexPath.section==0) { 

      if (indexPath.row==0) { 
       productImage=[self createImageWithFrame: CGRectMake(115.5, 10, 89,89)]; 
       productImage.tag=indexPath.row*indexPath.section+9999; 
       [cell.contentView addSubview: productImage]; 
      } 
      else{ 
       titleLabel1=[self createLabelWithFrame:CGRectMake(4, 5, 100, 30)];////normal label 
       titleLabel1.tag=indexPath.row*indexPath.section+9999; 
       [cell.contentView addSubview:titleLabel1]; 
       titleLabel1.text=[labelArray objectAtIndex:indexPath.row-1]; 
       if (indexPath.row==2) { 
       productImage=[self createImageWithFrame: CGRectMake(104, 10, 190,20)]; 
       productImage.tag=indexPath.row*indexPath.section+9999;; 
       [cell.contentView addSubview: productImage]; 

       } 
       else if (indexPath.row==7) { 
       titleLabel1=[self createLabelWithFrame:CGRectMake(4, 5, 100, 30)];////time label 
       titleLabel1.tag=indexPath.row*indexPath.section+9999; 
       [cell.contentView addSubview:titleLabel1]; 
       } 
       else if (indexPath.row==8) { 
       titleLabel1=[self createLabelWithFrame:CGRectMake(4, 5, 100, 30)];///time label 
       titleLabel1.tag=indexPath.row*indexPath.section+9999; 
       [cell.contentView addSubview:titleLabel1]; 
       } 
       else if (indexPath.row==9) { 
        descriptionText=[[UITextView alloc]initWithFrame:CGRectMake(104, 10, 190, 50)]; 
        descriptionText.tag =indexPath.row*indexPath.section+9999; 
        [cell.contentView addSubview:descriptionText]; 
       } 
       else{ 
       textFld=[self createTextFieldWithFrame:CGRectMake(104, 10, 190, 20)]; 
       textFld.tag=indexPath.row*indexPath.section+9999; 
       [cell.contentView addSubview:textFld]; 
        textFld.text=[labelArray objectAtIndex:indexPath.row*indexPath.section-1];//check this line I dont have idea about your array. 
       } 
      } 

     } 
    } 

    ///////trying to resuse the cell 
    if (indexPath.section==0) { 
     if (indexPath.row==0) { 
      productImage=(UIImageView *)[cell.contentView viewWithTag:indexPath.row*indexPath.section+9999;]; 
      //productImage.image= 
     } 
     else{ 
      titleLabel1=(UILabel *)[cell.contentView viewWithTag:indexPath.row*indexPath.section+9999;]; 
      titleLabel1.text=[labelArray objectAtIndex:indexPath.row+indexPath.section-1]; 
      if (indexPath.row==2) { 
       // productImage=[self createImageWithFrame: CGRectMake(104, 10, 190,20)]; 


      } 
      if (indexPath.row==7) { 
       // titleLabel1=(UILabel *)[cell.contentView viewWithTag:indexPath.row*indexPath.section+9999;];////time label 


      } 
      if (indexPath.row==8) { 
       // titleLabel1=(UILabel *)[cell.contentView viewWithTag:indexPath.row*indexPath.section+9999;];;///time label 


      } 
     // textFld=(UITextField *)[cell.contentView viewWithTag:indexPath.row*indexPath.section+9999;]; 

      if (indexPath.row==9) { 

      } 
     } 

    } 

你錯過了:return cell;