【问题标题】:UITableViewCell with image on the right?UITableViewCell 右侧有图像?
【发布时间】:2010-10-21 07:00:22
【问题描述】:

有没有办法将 UITableViewCell.image 设置为显示在单元格的右侧而不是左侧?或者我需要在单元格布局的右侧添加一个单独的 UIImageView 吗?

【问题讨论】:

    标签: iphone objective-c uitableview uiimageview uiimage


    【解决方案1】:

    没有。但是您可以轻松地将图像视图作为附件视图添加到表格单元格以获得相同的效果。

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
    cell.accessoryView = imageView;
    [imageView release];
    

    【讨论】:

    • 会改变附件视图然后覆盖显示指示器吗?
    • 是的,它会的。如果需要,则必须在创建时手动将 UIImageView 添加到单元格中。或者为您的特殊表格单元格创建一个子类,为您很好地包装所有内容。
    • UIImage 方法中有一个小错字:应该是 [UIImage imageNamed:@"foo.png"]
    • 很抱歉唤醒了这个线程,但它没有在起跑线上工作,我必须在发布之前和设置附件视图属性之后添加[imageView sizeToFit];,希望这会有所帮助:)
    • 通过对 contentView 应用变换,您可以将 imageView 放置在右侧。您还需要应用转换来翻转 imageView 和任何标签。
    【解决方案2】:

    对于简单的情况,您可以这样做:

    cell.contentView.transform = CGAffineTransformMakeScale(-1,1);
    cell.imageView.transform = CGAffineTransformMakeScale(-1,1);
    cell.textLabel.transform = CGAffineTransformMakeScale(-1,1);
    cell.textLabel.textAlignment = NSTextAlignmentRight; // optional
    

    这将翻转(镜像)内容视图,将任何 imageView 放置在右侧。请注意,您必须翻转 imageView 和任何文本标签,否则它们本身将被镜像!此解决方案保留右侧的附件视图。

    【讨论】:

      【解决方案3】:

      这是 Swift 中使用accessoryView 的示例:

      private let reuseIdentifier: String = "your-cell-reuse-id"
      
      // MARK: UITableViewDataSource
      
      func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell
        if (cell == nil) {
          cell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:reuseIdentifier)
        }
      
        cell!.textLabel!.text = "Hello"
        cell!.detailTextLabel!.text = "World"
        cell!.accessoryView = UIImageView(image:UIImage(named:"YourImageName")!)
        return cell!
      }
      

      【讨论】:

        【解决方案4】:

        如果您不想制作自定义单元格并使用标准单元格,您至少有两种方法:

        1. 使用附件视图。
        cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
        cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
        
        1. 如果你想拥有正确的图片+附件视图,那么你可以将 UIImageView 出口添加到 ContentView(示例名称:rightImage)并将 textLabel 的背景颜色更改为清除。
        cell.rightImage.image = UIImage(named: "imageName")
        cell.textLabel.backgroundColor = UIColor.clearColor()
        

        【讨论】:

          【解决方案5】:

          swift3swift4 中,我们可以使用这个:

          cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)
          

          【讨论】:

            【解决方案6】:

            继承 UITableViewCell 并覆盖 layoutSubviews,然后只调整 self.textLabel、self.detailTextLabel 和 self.imageView 视图的框架。

            这可能是代码中的样子:

            MYTableViewCell.h:

            #import <UIKit/UIKit.h>
            
            @interface MYTableViewCell : UITableViewCell
            @end
            

            MYTableViewCell.m:

            #import "MYTableViewCell.h"
            
            @implementation MYTableViewCell
            
            - (void)layoutSubviews
            {
                [super layoutSubviews];
                if (self.imageView.image) {
                    self.imageView.frame = CGRectMake(CGRectGetWidth(self.contentView.bounds) - 32 - 8,
                                                      CGRectGetMidY(self.contentView.bounds) - 16.0f,
                                                      32,
                                                      32);
                    CGRect frame = self.textLabel.frame;
                    frame.origin.x = 8;
                    self.textLabel.frame = frame;
                    frame = self.detailTextLabel.frame;
                    frame.origin.x = 8;
                    self.detailTextLabel.frame = frame;
                }
            }
            
            @end
            

            【讨论】:

              【解决方案7】:

              要在单元格中添加图像@右侧,我建议您创建一个自定义单元格,右侧带有图像视图,这对您非常有用。 并添加一个空视图以将此 nib 与 customcell 类链接。

              现在在您的单元格的 nib 文件中删除视图,添加 tableViewcell 并在该单元格中将 imageView 拖放到右侧。

              现在去 TableViewCell 的类说 DemoCell ,创建一个 outlet 并使用 tablecell 笔尖中的 imageview 设置它

              现在,在 tableview 的 cellforrowatindexpath 方法中引用您的单元格并将其与这样的笔尖名称一起使用

              static NSString *CellIdentifier = @"Cell";
              DemoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
              if (cell == nil) {
                  cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                  cell = [[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil] lastObject];
              }
              

              并根据您的要求设置图像

              cell.imageVw.img ....

              【讨论】:

                【解决方案8】:

                您可以通过调用 imageview 的 setFrame 方法来调整附件视图中的图像大小,如下所示:[imageView setFrame:CGRectMake(0, 0, 35.0, 35.0)];

                【讨论】:

                  【解决方案9】:

                  此 Soln 用于在每个单元格中添加不同的图像....只是尝试

                  // Customize the appearance of table view cells.
                  - (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];
                  }
                  
                  if(indexPath.row == 0) {
                      cell.image = [UIImage imageNamed:@"image.png"];
                  }
                  
                  else if (indexPath.row == 1) {
                      cell.image = [UIImage imageNamed:@"image1.png"];
                  }
                  

                  【讨论】:

                  • 与问题无关?
                  • OP 已经了解如何使用单元格的图像视图添加图像,这就是 sn-p 显示的全部内容。如何将图像定位在右侧是个问题。
                  【解决方案10】:

                  无需创建自己的图片,只需编辑cell.tintColor即可。

                  【讨论】:

                    【解决方案11】:

                    有一种方法可以以编程方式设置position。这是Swift 版本的解决方案:

                    image.frame = CGRect.init(
                    x: self.view.frame.width - image.frame.width*1.1,
                    y: image.frame.origin.y,
                    width: image.frame.width,
                    height: image.frame.height)
                    

                    这会将您的图像放置在单元格的右侧。 该解决方案是根据屏幕尺寸自动调整位置。 唯一的缺点是当您旋转设备时需要重新加载数据,但您可以在 UITableView 类中使用 override didRotate 侦听器方法:

                    override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
                        _tableView.reloadData()}
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2014-04-21
                      • 2021-03-16
                      • 1970-01-01
                      • 2018-07-28
                      • 2017-01-25
                      • 2022-11-12
                      • 1970-01-01
                      相关资源
                      最近更新 更多