【问题标题】:More than one image on UITableViewUITableView 上不止一张图片
【发布时间】:2012-03-03 15:40:54
【问题描述】:

我需要将两张图片放入 iOS 表格视图中。

我使用此代码将图像放置在单元格中:

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

UIImage *cellImage = [UIImage imageNamed:@"verde_diversos.png"];
cell.imageView.image = cellImage;
cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]]; 

return cell;
}

帮助?

【问题讨论】:

    标签: iphone ios image ipad uitableview


    【解决方案1】:

    您需要创建自己的UITableViewCell 子类。在其初始化程序中,您将设置两个 UIImageViews 并将它们添加到 contentView 作为它的子视图。然后在layoutSubviews 中,您将根据您希望它们的布局方式设置UIImageViews 的框架。

    【讨论】:

      【解决方案2】:
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
      static NSString *CellIdentifier = @"Cell";
      
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      
      UIImageView *cellView = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)] autorelease];
      cellView.tag = 555;
      
      [cell.contentView addSubview:cellView];
      }
      
      UIImage *cellImage = [UIImage imageNamed:@"verde_diversos.png"];
      cell.imageView.image = cellImage;
      cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]]; 
      
      UIImageView *secondImage = [cell.contentView viewWithTag:555];
      secondImage.image = [UIImage imageNamed:@"logo.png"];
      
      return cell;
      }
      

      【讨论】:

      • 如果你将 UIImageView 添加到 cell.contentView 我会支持你。单元格的子视图应进入 contentView,以便单元格的拉伸/收缩(例如,当您添加附件视图时)正常工作
      猜你喜欢
      • 1970-01-01
      • 2013-08-16
      • 2012-03-09
      • 2021-12-01
      • 1970-01-01
      • 2013-07-27
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多