【问题标题】:Image in the Tableview cell表格视图单元格中的图像
【发布时间】:2016-07-27 07:40:04
【问题描述】:

您好,我是 iOS 新手。

我在 ViewController1 中实现了一个表格视图。在表格视图中,我正在显示单元格的标题、详细信息和披露指示符。

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                   reuseIdentifier:MyIdentifier];
}



cell.textLabel.text =[listTableArray objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor grayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

  cell.detailTextLabel.text=@"4mile";





return cell;
}

一切正常,现在我需要在披露指示器之前使用图像

 UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
  accessoryView.image=[UIImage imageNamed:@"list.png"];
cell.accessoryView = accessoryView;

披露指示符被图像替换。但我需要两个图像以及披露指示符,而不使用自定义单元格。

我该怎么做...?帮帮我。

谢谢

类似于截止日期,但在那个时间我需要图像

【问题讨论】:

  • 请提供屏幕截图显示图像的位置
  • 确实应该是自定义单元格
  • @Birendra 添加了屏幕截图
  • 您好,您有解决方案吗?

标签: ios objective-c uitableview uiimageview


【解决方案1】:

UITableViewCell 有一个固定的布局,取决于你初始化它时使用的样式。您无法更改该布局,因为单元格会按照自己喜欢的方式布置其子视图。

- (void)layoutSubviews 添加到您的单元子类中,如下所示:

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0.0f , 0.0f, 20.0f, 20.0f);
}

试试这个

 UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"list.png"];;
[imageView setFrame:CGRectMake(50, 5, 20, 20)]; // customize the frame
 [cell.contentView addSubview:imageView];

【讨论】:

  • @Anbu anna 我已尝试自定义框架,它与单元格的详细文本重叠。
  • @Cyph3r - 然后更改 x 坐标并尝试,肯定有效
  • @Anbu 我已经尝试更改 x 值,它有效我需要详细文本和披露箭头之间的图像,当 x 值出现时它会隐藏。
  • 但我需要它们之间的图像
  • @Cyph3r - 兄弟,我不知道你的概念,但我相信你改变 x 和 y 坐标肯定会工作,一切顺利
【解决方案2】:

如果您想将UIImageView 添加到标准UITableViewCell 您需要将您的imageView 添加到单元格的contentView 并添加所有子视图,例如textLabeldetailTextLabelaccessoryView

UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
accessoryImageView.image=[UIImage imageNamed:@"list.png"];
[cell.contentView addSubview:accessoryImageView];

UITableViewCell 的子类并在其中实现所有视图

【讨论】:

  • 您的图片没有显示?或坠毁
  • 图像来了但隐藏了披露
  • 更改你的accessoryView的名字,标准的tableviewcell有一个同名的uiview
  • 图像重叠在披露箭头和详细文本上
  • 是的,对于这种情况,您需要创建一个自定义的 uitableviewcell
猜你喜欢
  • 2014-01-13
  • 2013-06-23
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 2018-04-19
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多