4

20行を含むを作成してTableViewいます。偶数のセルに単一のラベルを追加し、奇数のセルに2つのラベルを追加する必要があります。目的のラベルを追加してテーブルをスクロールすると、下に行くとラベルが消えます。 。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // UITableView *cell=[tableView ]
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if(indexPath.row%2==0)
    {
        UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
        cellLabel.text=@"o1";
        [cell addSubview:cellLabel];
        [cellLabel release];
    }
    else
    {
        UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
        cellLabel1.text=@"e1";
        [cell addSubview:cellLabel1]; 
        [cellLabel1 release];
        UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)]; 
        cellLabel2.text=@"e2";
        [cell addSubview:cellLabel2]; 
        [cellLabel2 release];
    }

}
4

6 に答える 6

1

これをチェックして。これは、各ラベルのY位置を変更しただけのコードと同じであり、スクロールでも正常に機能します。

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

{static NSString * CellIdentifier = @ "Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// UITableView *cell=[tableView ]
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if(indexPath.row%2==0)
    {
        UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 40, 40)]; 
        cellLabel.text=@"o1";
        [cell addSubview:cellLabel];

    }
    else
    {
        UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 40, 40)]; 
        cellLabel1.text=@"e1";
        [cell addSubview:cellLabel1]; 

        UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 10, 40, 40)]; 
        cellLabel2.text=@"e2";
        [cell addSubview:cellLabel2]; 

    }

}
return cell;

}

于 2012-06-29T10:39:25.363 に答える
0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{{
    static NSString * MyIdentifier = @ "MyIdentifier";
    static NSString * MyIdentifier1 = @ "MyIdentifier1";
    UITableViewCell*セル;
    if(indexPath.row%2!= 0)
    {{
        cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    }
    そうしないと
    {{
        cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier1];
    }
    if(cell == nil)
    {{
        if(indexPath.row%2!= 0)
        {{
            cell = [self restartTableViewCellWithIdentifier:MyIdentifier withIndexPath:indexPath];
        }
        そうしないと
        {{
            cell = [self restartTableViewCellWithIdentifier:MyIdentifier1 withIndexPath:indexPath];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    セルを返します。
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{{
    UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault restartIdentifier:identifier] autorelease];
    if([identifier isEqualToString:@ "MyIdentifier"])
    {{

        UILabel * l1 = [[[UILabel alloc] initWithFrame:CGRectMake(5、5、30、20)] autorelease];
        l1.backgroundColor = [UIColor redColor];
        [cell.contentView addSubview:l1];
        UILabel * l2 = [[[UILabel alloc] initWithFrame:CGRectMake(65、5、30、20)] autorelease];
        l2.backgroundColor = [UIColor grayColor];
        [cell.contentView addSubview:l2];       
    }
    そうしないと
    {{
        UILabel * l1 = [[[UILabel alloc] initWithFrame:CGRectMake(5、5、30、20)] autorelease];
        l1.backgroundColor = [UIColor blueColor];
        [cell.contentView addSubview:l1];
    }
    セルを返します。
}
于 2012-08-22T09:29:22.147 に答える
0

このようにしてください:

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



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // UITableView *cell=[tableView ]
    if (cell == nil)
    {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
        cellLabel.text=@"o1";
cellLabel.tag = 2;
        [cell addSubview:cellLabel];
        [cellLabel release];

        UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
        cellLabel1.text=@"e1";
cellLable1.tag = 3;
        [cell addSubview:cellLabel1]; 
        [cellLabel1 release];
        UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)]; 
        cellLabel2.text=@"e2";
cellLabel2.tag = 4;
        [cell addSubview:cellLabel2]; 
        [cellLabel2 release];
    }

UILabel *label1 = (UILabel *) [cell viewWithTag:2];
UILabel *label2 = (UILabel *) [cell viewWithTag:3];
UILabel *label3 = (UILabel *) [cell viewWithTag:4];

if (indexPath.row%2==0)
{
label1.hidden = FALSE;
label2.hidden = TRUE;
label3.hidden = TRUE:
}
else 
{
label1.hidden = TRUE;
label2.hidden = FALSE;
label3.hidden = FALSE;
}

    }
于 2012-06-29T08:08:16.600 に答える
0

2種類のセルは異なるので、それぞれに異なるセル識別子を作成します。何かのようなもの:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *EvenCellIdentifier = @"EvenCell";
    static NSString *OddCellIdentifier = @"OddCell";
    NSString* cellIdentifier = (indexPath.row % 2) == 0 ? EvenCellIdentifier : OddCellIdentifier;

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

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if(indexPath.row%2==0) 
        {
            UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
            cellLabel.text=@"o1";
            [cell addSubview:cellLabel];
            [cellLabel release];
        }
        else
        {
            UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
            cellLabel1.text=@"e1";
            [cell addSubview:cellLabel1]; 
            [cellLabel1 release];
            UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)]; 
            cellLabel2.text=@"e2";
            [cell addSubview:cellLabel2]; 
            [cellLabel2 release];
        }
    }

    return cell;
}
于 2012-06-29T09:30:23.423 に答える
0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{{
   if(indexPath.row%2 == 0)
   {{
    static NSString * CellIdentifier = @ "CustomCell";

    CustomCell * cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {{
        NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@ "CustomCell" owner:self options:nil];
        for(id currentObject in topLevelObjects)
        {{
            if([currentObject isKindOfClass:[UITableViewCell class]])
            {{
                cell =(CustomCell *)currentObject;
                壊す;
            }
        }
    }
    cell.capitalLabel.text = [capitals objectAtIndex:indexPath.row];
    cell.stateLabel.text = [states objectAtIndex:indexPath.row];
    cell.t1.delegate = self;
    cell.t1.tag = indexPath.row;
    cell.t1.text = [arrTemp objectAtIndex:indexPath.row];
    cell.s1.backgroundColor = [UIColor grayColor];
    cell.s1.contentSize = CGSizeMake(1000、40);
    セルを返します。
   }
    そうしないと
    {{
        static NSString * CellIdentifier1 = @ "CustomCell1";

        CustomCell1 * cell1 =(CustomCell1 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if(cell1 == nil)
        {{
            NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@ "CustomCell1" owner:self options:nil];
            for(id currentObject in topLevelObjects)
            {{
                if([currentObject isKindOfClass:[UITableViewCell class]])
                {{
                    cell1 =(CustomCell1 *)currentObject;
                    壊す;
                }
            }
        }
        cell1.l1.text = @ "alok";
        cell1を返します。

    }

}

于 2012-08-22T09:31:20.270 に答える
0
      static NSString *CellIdentifier = @"Cell";
  static NSString *CellIdentifierButton = @"CellButton";
   UITableViewCell *cell = nil;




      if (indexPath.section <=2) 
      {
        if(indexPath.section==2&&indexPath.row==4)
        {
            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierButton]; 

        }
        else
        {
            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        }
     }   
    if (cell == nil)
    {   
        if (indexPath.section <=2)
        {
            if(indexPath.section==2&&indexPath.row==4)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierButton];
                UIButton *b1 = [[UIButton alloc]initWithFrame:CGRectMake(200, 10, 300, 80)];
                [b1 setBackgroundColor:[UIColor redColor]];
                [b1 setTag:501];           
                [cell.contentView b1];

                UIButton * b2 = [[UIButton alloc]initWithFrame:CGRectMake(200, 120, 300, 80)];
                [b2 setBackgroundColor:[UIColor redColor]];
                [b2 setTag:502];           
                [cell.contentView b2];

            }
            else
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 250, 44)];
                [label setBackgroundColor:[UIColor clearColor]];
                [label setTag:500];           
                [cell.contentView addSubview:label];

                //end for section 1

                //for section2



                }
                //end for section 2

            }


        }
    }




    //====setting the values===//
    if (indexPath.section ==0) 
    {
        UILabel *label = (UILabel *)[cell.contentView viewWithTag:500];
        if (label) {
            [label setText:[arr1 objectAtIndex:indexPath.row]];
        }
    }
    if (indexPath.section ==1) 
    {
        UILabel *label = (UILabel *)[cell.contentView viewWithTag:500];
        if (label) {
            [label setText:[arr2 objectAtIndex:indexPath.row]];
        }
    }
于 2013-03-01T04:14:18.977 に答える