【问题标题】:adding images to UItableView将图像添加到 UItableView
【发布时间】:2011-06-27 07:23:15
【问题描述】:

是否可以将图像添加到表格视图中?到左边?如果是这样,它应该是什么尺寸?

【问题讨论】:

    标签: iphone cocoa-touch uikit uitableview


    【解决方案1】:

    自定义 UITableViewCell 不需要简单地将图像添加到单元格的左侧。只需在 tableView:cellForRowAtIndexPath: 委托方法中配置 UITableView 单元格的 imageView 属性,如下所示:

    - (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];
    
       cell.textLabel.text = @"I'm a UITableViewCell!";
       cell.imageView.image = [UIImage imageNamed:@"MyReallyCoolImage.png"];
    
       return cell;
    }
    

    除非您在 UITableViewDelegate 中提供 tableView:heightForRowAtIndexPath: 方法,否则 UITableViewCell 的默认高度为 44 磅,在非视网膜显示器上为 44 像素,在视网膜显示器上为 88 像素。

    【讨论】:

    • @thomas:这很有帮助也很重要
    • 你提到了 UITableViewCell 本身的高度,这 44 个点与 UIImageView 的大小无关,如要求的那样......
    • @ingenspor,由于单元格的高度为 44 点,因此图像视图的高度为 44 点。
    【解决方案2】:

    是的,这是可能的。您可以从cocoawithlovehere 获得帮助。这些教程将让您了解如何将图像提供给UITableView。最后,正如之前在 SO 上询问的那样,UITableViewCell Set Selected Image

    【讨论】:

      【解决方案3】:

      是的,您可以在单元格中的任意位置添加它们。最后,它们应该是对您的应用有意义的大(或小)。

      【讨论】:

      • 是的,请参阅 tableview 套件示例代码了解如何创建自定义单元格。将图像视图放置在您的自定义单元格上您希望它所在的位置,当您设置您的 tableview 数据源时,使用此自定义视图,将图像添加到图像视图中,然后就开心了。
      【解决方案4】:

      Swift 4托马斯答案的解决方案:

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let CellIdentifier = "Cell"
          var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: CellIdentifier)
          if cell == nil {
              cell = UITableViewCell(style: .default, reuseIdentifier: CellIdentifier)
          }
          cell?.textLabel?.text = "I'm a UITableViewCell!"
          cell?.imageView?.image = UIImage(named: "MyReallyCoolImage.png")
          return cell ?? UITableViewCell()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        • 1970-01-01
        相关资源
        最近更新 更多