在自定义单元格中动态添加图像视图并为其分配图像

时间:2013-05-23 11:32:51

标签: iphone ios ios6

我面临很多问题..我是ios开发的新手,所以感谢任何帮助。

dyanamic imageview 第一个问题

我想在黄色和蓝色图像上显示N个图像视图+标签在自定义单元格中。

我必须解析每行上将显示多少个图像视图,并且根据我必须将图像分配给我的自定义单元格中的每个图像视图。我怎样才能做到这一点? 我从数据库中获取图像链接并将其存储到数组中,但如何将链接应用于特定的图像视图。

只需要在iOS中构建上述用户界面的提示。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifierLeft = @"lefthand";
static NSString *CellIdentifierRight = @"righthand";

if (indexPath.row % 2 == 1) // to get two custom cell vice versa.
{
  LeftCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierLeft];


    if (LeftCell == nil)
   { 
        LeftCell = [[customCellLeft alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLeft row:indexPath.row];


         LeftCell.scrollView  = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(0,14,1024,187)];


    }
      LeftCell.videoImageView1.tag=[ImageLinkArray objectAtIndex:indexPath.row];
       LeftCell.label1.text=[TitleArray objectAtIndex:indexPath.row];

    return LeftCell;
}
  else
{

RightCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierRight];


if(RightCell == nil)
{

    RightCell =[[customCellRight alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierRight];

   RightCell.scrollView  = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(0,14,1024,187)];


    }
      RightCell.videoImageView1.tag=[ImageLinkArray objectAtIndex:indexPath.row];
       RightCell.label1.text=[TitleArray objectAtIndex:indexPath.row];

    return RightCell;
}

自定义单元格类代码......

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier row:(int)row
 {
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {

    int indexNumber; // variable to store Array index number

    int VideoNumber=3; // imageview in particular cell + Train Engine.


   scrollView  = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(33,14,958,187)]; // Scrollview 
    scrollView.scrollEnabled=YES;
    scrollView.userInteractionEnabled=YES;
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.contentSize = CGSizeMake((335*VideoNumber),187); // To make ContentSize for item to scroll
      for (int i = 0; i < VideoNumber; i++)
    { // for loop to add 2 wagon view and 1 engine view on Left hand side Custom cell.

        if(i==0) // to check first item must be train Engine
        {
            CGRect frame;
            frame.origin.x = 0;
            frame.origin.y = 18;
            frame.size.width=186;
            frame.size.height= 162;

            UIView *EngineView = [[UIView alloc]initWithFrame:frame];
            UIImageView *engineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 6, 186, 162)];
            UIImage *Image = [UIImage imageNamed:@"loco_left.png"];
            engineImageView.image = Image;
            [EngineView addSubview:engineImageView];
            [scrollView addSubview:EngineView];  // add 1st imageview that is Train Engine to first of scrollview
        }
        else

        { // else it must be wagon of train.

            indexNumber=row*(VideoNumber-1)+(i-1);// to get Index Number of array to to display Images as per cell number

            NSLog(@"at Normal row index number is %d",indexNumber);
            CGRect frame;
            frame.origin.x = (385*i)-432;
            frame.origin.y = 18;
            frame.size.width=385;
            frame.size.height= 163;

            UIView *wagon = [[UIView alloc]initWithFrame:frame];
            UIImageView *wagonImage = [[UIImageView alloc]initWithFrame:CGRectMake(230, 6, 385, 163)];
            UIImage *Image = [UIImage imageNamed:@"wagon_left.png"];
            wagonImage.image = Image;
            [wagon addSubview:wagonImage];


            UIView *videoviewContainer = [[UIView alloc]initWithFrame:CGRectMake(15, 30, 190 , 200)];

            videoImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 190, 100)];
            UIImage *bgImage = [UIImage imageNamed:@"video.png"];
            videoImageView2.image = bgImage;
            videoviewContainer.contentMode = UIViewContentModeLeft; // set Video Image view to left hand side of wagon UIView
            videoImageView2.tag=indexNumber;

            [videoviewContainer addSubview:videoImageView2];

            UITapGestureRecognizer *video = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(VideoItemOne:)];
            [videoImageView2 addGestureRecognizer:video];



            [videoImageView2 setMultipleTouchEnabled:YES];
            [videoImageView2 setUserInteractionEnabled:YES];

            UIView *textUiView = [[UIView alloc]initWithFrame:CGRectMake(208,28, 150 , 187)];
            textUiView.contentMode = UIViewContentModeRight;
            label2 = [[UILabel alloc]initWithFrame:CGRectMake(28,10, 150 , 87)];
            label2.text=[TitleArray objectAtIndex:indexNumber];
            label2.backgroundColor = [UIColor clearColor];
            label2.textColor=[UIColor redColor];
            label2.numberOfLines = 4;
            [textUiView addSubview:label2];

            [wagonImage addSubview:textUiView];
            [wagonImage addSubview:videoviewContainer];

            [scrollView addSubview:wagon];

        }
    }


    [self.contentView addSubview:scrollView];
     }
return self;
  }
-(void)populateWithImage:myImage andText:myText
{
[videoImageView2 setImageWithURL:[NSURL URLWithString:myImage]
                placeholderImage:[UIImage imageNamed:@"video.png"]];
  }

1 个答案:

答案 0 :(得分:0)

您的自定义单元格应具有以下内容:(注意,您必须使用更好的命名约定,我只是举个例子)

  • 背景Imageview(bgImage)
  • 主图像查看(mainImage)
  • UI标签(mainLabel)

在此单元格的init中,基于indexpath.row,指定左或右列车图像。

还设置主图像视图和标签的帧。

        MYCustomCell * cell;

        if(indexPath.row %2)
        {
        cell = [tableView dequeueReusableCellWithIdentifier:"OddCell"];
            if (cell == nil)
             { 
                cell = [[MYCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLeft row:indexPath.row];
             }
        }
        else {
             cell = [tableView dequeueReusableCellWithIdentifier:"EvenCell"];
            if (cell == nil)
             { 
                cell = [[MYCustomCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifierLeft row:indexPath.row];
             }
        }


    [cell populateWithImage:myImage andText:myText]; //myImage and myText are to be gotten from the data source which in your case is ImageLinkArray and TitleArray
return cell;