在TableView Cell中添加图像不会显示

时间:2013-07-19 08:07:31

标签: ios uitableview uikit

我在tableVIew单元格中添加图片,但它没有显示。 我正在使用if条件将图像赋予按钮,但它没有显示。

以下是按钮添加的代码

    addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.Frame=CGRectMake(590,2,42,42);

    [addButton addTarget:self action:@selector(tab1Action:) forControlEvents:UIControlEventTouchUpInside];
    addButton.tag=indexPath.row;


    NSString*test=theData.Status;

    if ([test isEqualToString:@"1"]) {

        test=@"Already Member of the group";



    }

    else {

        test=@"";



        NSLog(@"Else is also Working Fine");




        [addButton setImage:[UIImage imageNamed:@"addbutton.png"] forState:UIControlStateNormal];

    }
    addButton.clipsToBounds=YES;
    [[cell contentView] addSubview:addButton];       

    cell.textLabel.font=[UIFont fontWithName:@"Helvetica" size:16];
    cell.textLabel.text=[NSString stringWithFormat:@"%@: %@",theData.GroupTitle,test];

    return cell;

3 个答案:

答案 0 :(得分:0)

您无需添加到单元格的内容视图。所以这样做..

   [cell addSubview:addButton]; 

答案 1 :(得分:0)

您的按钮框架CGRectMake(590,2,42,42)是错误

frame.origin.x太大于屏幕尺寸

在开发中只有320,480的大小

640,960是自动的

答案 2 :(得分:0)

//为app的不同部分创建一个表

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * CellIdentifier = @“Cell”;

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(!cell){

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    

    }

    / *重要提示:由于我们使用的是“动态类型”,因此当表视图重新加载时,我们必须先删除它  容器中的视图。  * /

    if([cell.contentView subviews]){     for((cell.contentView子视图)中的UIView *子视图){         [subview removeFromSuperview];     } }

    //为图像视图创建一个矩形框 UIImageView * iconImage = [[UIImageView alloc] init]; iconImage.frame = CGRectMake(12,4,53.5,53.5);

    //为单元格创建标签 UILabel * cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)]; cellName.font = [self fontForBodyTextStyle];

    NSString * cellNameStr = [NSString stringWithFormat:@“%@”,self.tableCellNames [indexPath.row]];

    //选择路径行选项卡操作 switch(indexPath.row){     案例0:

        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"condition.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
    
        break;
    
    case 1:
       cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"videos.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;
    
    case 2:
        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"provider.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;
    
    case 3:
        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"info.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;
    
    default:
        NSAssert(NO, @"Unhandled value in cellForRowAtIndexPath");
        break;
    

    }

    返回细胞; } 谢谢VKJ,