-1

我有一个包含 30 个字段(标签、文本字段、星级和其他一些 UIView、picercontrol)的表格视图。问题是当我滚动表格视图时,单元格被替换了。有什么解决方案吗?编辑:

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

3 回答 3

2

问题在于设置标签值:使用indexPath.row*indexPath.section+9999(some value);

尝试这个:

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

于 2013-06-13T05:31:32.267 回答
1

The problem is due to in proper tagging of views inside the cell. In your implementation first row of zeroth section and zeroth row of first section will be tagged with same number.

You should tag the views by below way:

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

Retrieve them by same way:

productImage=(UIImageView *)[cell.contentView viewWithTag:indexPath.row+indexPath.section*1000];
于 2013-06-13T05:25:23.457 回答
0

当您滚动表格视图时,相应的单元格会在内部调用 reload 方法。为避免这种情况,您应该使用可重复使用的单元格:

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

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
于 2013-06-13T03:52:22.457 回答