【问题标题】:label is not showing on tableView cell标签未显示在 tableView 单元格上
【发布时间】:2012-06-29 08:01:11
【问题描述】:

我正在创建一个包含 20 行的 TableView。我必须向偶数单元格添加单个标签,并且我必须在奇数单元格中添加两个标签。当我添加所需的标签并滚动我的表格时,标签消失为我下去帮帮我。

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

}

【问题讨论】:

标签: iphone objective-c uitableview ios5


【解决方案1】:

检查这个。这与您的代码相同,我刚刚更改了每个标签的 Y 位置,并且滚动也可以正常工作。

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

{ 静态 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;

}

【讨论】:

  • 如果您使用默认行高,那么您的标签将移出相关单元格。正如您给出的 Y=40 和默认行高为 44。
【解决方案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;
}

【讨论】:

    【解决方案3】:

    这样做:

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

    【讨论】:

    • 删除所有标签的发布声明,然后再试一次,我相信它会起作用
    【解决方案4】:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态 NSString *MyIdentifier = @"MyIdentifier"; 静态 NSString *MyIdentifier1 = @"MyIdentifier1"; UITableViewCell *cell; if(indexPath.row % 2 != 0) { 单元格 = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; } 别的 { 单元格 = [tableView dequeueReusableCellWithIdentifier:MyIdentifier1]; } 如果(细胞 == 零) { if(indexPath.row % 2 != 0) { 单元格=[自我重用TableViewCellWithIdentifier:MyIdentifier withIndexPath:indexPath]; } 别的 { 单元格=[自我重用TableViewCellWithIdentifier:MyIdentifier1 withIndexPath:indexPath]; } } cell.selectionStyle = UITableViewCellSelectionStyleNone; 返回单元格; } -(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; if([标识符 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]; } 返回单元格; }

    【讨论】:

      【解决方案5】:
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row%2==0) { 静态 NSString *CellIdentifier = @"CustomCell"; CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 如果(单元格 == 零) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell 类]]) { cell = (CustomCell *) currentObject; 休息; } } } cell.capitalLabel.text =[大写对象AtIndex:indexPath.row]; cell.stateLabel.text = [状态 objectAtIndex:indexPath.row]; cell.t1.delegate=自己; cell.t1.tag=indexPath.row; cell.t1.text=[arrTemp objectAtIndex:indexPath.row]; cell.s1.backgroundColor=[UIColor grayColor]; cell.s1.contentSize=CGSizeMake(1000, 40); 返回单元格; } 别的 { 静态 NSString *CellIdentifier1 = @"CustomCell1"; CustomCell1 *cell1 = (CustomCell1 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 如果 (cell1 == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell1" owner:self options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell 类]]) { cell1 = (CustomCell1 *) currentObject; 休息; } } } cell1.l1.text=@"alok"; 返回单元格1; } }

      【讨论】:

        【解决方案6】:
              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]];
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-14
          • 1970-01-01
          • 2019-10-27
          相关资源
          最近更新 更多