代码之家  ›  专栏  ›  技术社区  ›  arachide

奇怪的问题在UITableViewCell上绘制标签

  •  0
  • arachide  · 技术社区  · 15 年前

    我使用uinavigationcontroller pushviewcontroller呈现一个viewcontroller,在viewcontroller的视图上有一个uitablview。 我使用下面的代码在uiTableViewCell上绘制uiLabel,但它不显示,只有当我点击uiLabel所在的行时,它才会出现,如果我点击另一行,它会再次消失。

    - (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {   
    
                static NSString *SimpleTableIdentifier1 = @"CellTableIdentifier";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier1 ];
        if  (cell == nil) 
        {
            cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero       
                                                     reuseIdentifier:SimpleTableIdentifier1] autorelease];
            switch([[myArray objectAtIndex:indexPath.row] getValueType])
            {
                case _PSToggleSwitchSpecifier:  
    
                {           
                    UISwitch *switchview=[[UISwitch alloc]init];
    
                    switchview.tag=11106 ;
                    cell.accessoryView= switchview;
                     //switchview.frame=CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
                    //[cell.contentView addSubview:switchview];
                    [switchview addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    
                    [switchview release];   
                    break;
                }
                case  _PSMultiValueSpecifier:
                {
                    cell.accessoryType=  UITableViewCellAccessoryDisclosureIndicator;//
    
                    //draw a label, but actually it does display,  
                    UILabel *labelCell =[ [UILabel alloc]init];
                    [labelCell setTag:11101];
                    labelCell.frame = CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
                    [labelCell setBackgroundColor:[UIColor clearColor]];
                    labelCell.textAlignment=UITextAlignmentRight;
                    [labelCell setFont:[UIFont systemFontOfSize:18.0]];
                    [labelCell setTextColor:[UIColor blackColor]];
                    [cell.contentView addSubview:labelCell];
                    [labelCell release];
    
                    break;
    
                }
            }       
        }    
    
        cell.text=@"test";
    
        switch([[myArray objectAtIndex:indexPath.row] getValueType])
        {
    
            case _PSToggleSwitchSpecifier:  
            {
                cell.accessoryType=UITableViewCellAccessoryNone;//
    
                UISwitch *tem=(UISwitch*) [cell.contentView  viewWithTag:11106];//cell.accessoryView;// 
                tem.on= true;
                break;
    
            }
            case  _PSMultiValueSpecifier:
            {
                UILabel *newLabelCell=(UILabel*)[cell.contentView viewWithTag: 11101];//intV];
                newLabelCell.text=@"Items";//it should display, but actually not, only when I tap the row "didselect" the text display
                break;
    
            }
        };
    
        return cell;
    }
    

    上面的代码显示uiswitch,但如果我使用代码

    switchview.frame=CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
    [switchview addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    [cell.contentView addSubview:switchview];
    

    替代 cell.accessoryView= switchview;

    uiswitch将与uilabel相同

    欢迎发表评论

    谢谢

    交互开发

    1 回复  |  直到 15 年前
        1
  •  0
  •   joshpaul    15 年前

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier