如何将uiview添加到cellForRowAtIndexPath?

时间:2015-11-12 04:07:26

标签: ios uitableview view uiview

我创建了mycell,在myTableview中我创建了一个

@property (strong ,nonatomic)NSArray *contentPictureArray;

这是:

self.contentPictureArray = @[[UIImage imageNamed:@"wallha@2x.png"],@"wallha@2x.png"],[UIImage imageNamed:@"wallha@2x.png"]];

我想使用cell.contentPictureView.view=[self.contentPictureArray objectAtIndex:indexPath.row];但不是真的,如何将mycell添加到cellForRowAtIndexPath?

cell.stateView = [[UIView alloc] initWithFrame:CGRectMake(XB, HeiB+NY, WidB, 0)];
    [cell addSubview:cell.stateView];

    cell.contentTextView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WidB, 0)];
    [cell.stateView addSubview:cell.contentTextView];//动态文字部分

    cell.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WidB, 0)];
    [cell.contentTextView addSubview:cell.contentLabel];//动态文字label

    cell.allContentBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, cell.contentLabel.frame.size.height, HeiB, 2*NX)];
    [cell.contentTextView addSubview:cell.allContentBtn];//全文显示按钮

    cell.contentPictureView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.stateView.frame.origin.y+2*NX, HeiB+NY, 0)];
    [cell.stateView addSubview:cell.contentPictureView];

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KBTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[KBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdent:@"cell"];
    }

    if(indexPath.row==0||indexPath.row==1){

        cell.iconImageView.image = [UIImage imageNamed:@"avatar.png"];

    }

    cell.contentLabel.text=[self.listArray objectAtIndex:indexPath.row];
    cell.nameLabel.text = [self.nameArray objectAtIndex:indexPath.row];
    cell.timeLabel.text  =[self.timeArray objectAtIndex:indexPath.row];
    cell.contentPictureView.=[self.contentPictureArray objectAtIndex:indexPath.row];

    return cell;


}

3 个答案:

答案 0 :(得分:0)

我不明白。 在你的代码中: [self.contentPictureArray objectAtIndex:indexPath.row]是UIImage

cell.contentPictureView.view是UIView?

所以你编码:

cell.contentPictureView.view = [self.contentPictureArray objectAtIndex:indexPath.row]

你怎么做?

答案 1 :(得分:0)

在cellForRowAtIndexPath

中添加这样的图像
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10,100, 200)];
imageView.backgroundColor = [UIColor clearColor];
imageView.image = [contentPictureArray objectAtIndex:indexPath.row] ;
[cell.contentView addSubview:imageView];

希望这会有所帮助......

答案 2 :(得分:0)

您可以使用图像名称初始化数组,如下所示。

self.contentPictureArray = @[@"wallha@2x","wallha@2x",@"wallha@2x"]];

现在使用以下代码:

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10,100, 100)];
imgView.backgroundColor = [UIColor clearColor];
imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[contentPictureArray objectAtIndex:indexPath.row] ofType:@"png"]] ;
[cell.contentView addSubview:imageView];

如果您还想要,也可以创建自定义单元格。