简体   繁体   English

尝试在表格视图上获取全尺寸单元格图像

[英]Trying to get full-size cell images on table view

So I have this going on by using regular image views and buttons to work: 因此,我通过使用常规图像视图和按钮来进行此操作:

在此处输入图片说明

But now I want it to bounce as the user scrolls, any ideas on how to get this going? 但是现在我希望它随着用户滚动而反弹,关于如何实现这一点的任何想法? I've tried using the table view but I can't figure out how to get the images to be the background. 我已经尝试过使用表格视图,但是我不知道如何获取图像作为背景。 Any perks between using a table view and the current method I'm using? 使用表格视图和当前使用的方法之间有什么好处吗? This is a learning experience for me, and that's the whole point of the project. 对我来说,这是一次学习经历,这就是项目的重点。 Any help would be appreciated, thanks in advance. 任何帮助将不胜感激,在此先感谢。

Here is one way to achieve what you want (change image names accordingly) : 这是实现所需功能的一种方法(相应地更改图像名称)

cell.contentView.backgroundColor = UIColor(patternImage: UIImage(named: "NewYork")!)

For example: 例如:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    switch indexPath.row{
    case 0:
        cell.contentView.backgroundColor = UIColor(patternImage: UIImage(named: "NewYork")!)
    case 1:
        cell.contentView.backgroundColor = UIColor(patternImage: UIImage(named: "LA")!)
    case 2:
        cell.contentView.backgroundColor = UIColor(patternImage: UIImage(named: "Miami")!)
    default:
        cell.contentView.backgroundColor = UIColor(patternImage: UIImage(named: "default")!)
    }
}

You can also set it inside of cellForRowAtIndexPath the same way. 您也可以以相同的方式在cellForRowAtIndexPath内部进行设置。

The disadvantage of this is the image either needs to be a repeating pattern or fit the cell size exactly which isn't very flexible for different size devices. 这样做的缺点是图像要么需要是重复图案,要么需要精确地适合像元大小,这对于不同大小的设备而言不是很灵活。


Another option is to use a custom table cell and create a UIImageView the same size as the cell. 另一种选择是使用自定义表格单元格并创建与单元格相同大小的UIImageView。 Here is a tutorial link describing how to create a custom cell. 这是一个教程链接,描述了如何创建自定义单元。 Now you can just set the image in your cellForRowAtIndexPath similar to this: 现在,您可以像这样在cellForRowAtIndexPath设置图像:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomCell", forIndexPath: indexPath) as! CustomTableViewCell

  switch indexPath.row{
    case 0:
        cell.img_Thumbnail?.image = UIImage(named: "NewYork")
    case 1:
        cell.img_Thumbnail?.image = UIImage(named: "LA")
    case 2:
        cell.img_Thumbnail?.image = UIImage(named: "Miami")
    default:
        cell.img_Thumbnail?.image = UIImage(named: "default")
    }

  return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM