ios - UIImageView图像出现两次

标签 ios objective-c swift uiimageview

我的 tableview 单元格中有一个 UIImageView,它扩展了 UITableViewCell。它在初始化期间创建了imageview,并且我在cellForRowAtIndexPath方法中设置了imageView.image。但是,由于图像的大小小于imageView,因此图像出现了两次,如下所示:enter image description here

为什么会发生这种情况?我知道有一种方法可以调整 UIImageView 的大小,但该单元格也在其他地方使用,我设置了不同的图像。请告诉我这个问题的原因和解决方案。谢谢!

编辑:更新了仍无法正常工作的cellForRowAtIndexPath代码。

    var cell: AutocomplterViewCell! = tableView.dequeueReusableCellWithIdentifier(AutocomplterViewCell.reuseIdentifier) as! AutocomplterViewCell
    if (cell == nil) {
        cell = AutocomplterViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: AutocomplterViewCell.reuseIdentifier)
    }
    if tableView == self.recentsTableView {
        let recentArr: [RecentSearch]? = NSUserDefaults.standardUserDefaults().getRecentSearches()
        let item: RecentSearch = recentArr![indexPath.row]
        cell.setTitle(item.title)
        cell.setAddress(item.subAddress)
        cell.setImageIcon("Clock")
    } else {
        let item: GMSAutocompletePrediction! = mapTasks.allResults[indexPath.row]
        let address: String? = item.attributedFullText.string
        if address != nil {
            let addressArr = address!.componentsSeparatedByString(",")
            let title: String = addressArr[0]
            let tempTitle = title+", "
            let full: String = address!.length > title.length+3 ? address!.substringFromIndex(tempTitle.endIndex) : address!
            cell.setTitle(title)
            cell.setAddress(full)
            cell.setImageIcon("GrayPin")
        }
    }
    return cell

和 AutoCompleterViewCell 类:

import Foundation

class AutocomplterViewCell: UITableViewCell {

class var reuseIdentifier: String {
    return NSStringFromClass(AutocomplterViewCell)
}

static let cellHeight: CGFloat = 60.0
let topMargin: CGFloat = 10.0
let bottomMargin: CGFloat = 10.0
let betweenMargin: CGFloat = 5.0
let leftMargin: CGFloat = 10.0
let rightMargin: CGFloat = 10.0
let fontSizeLarge: CGFloat = 14.0
let fontSizeSmall: CGFloat = 11.0
let borderWidth: CGFloat = 1.0

var contentHeight: CGFloat {
    return AutocomplterViewCell.cellHeight - topMargin - bottomMargin
}

var predictionTitle = UILabel()
var predictionFull = UILabel()
var icon = UIImageView()
var borderLine = UIView()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.setupView()
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupView() {
    let iconHeight: CGFloat = 20
    let titleHeight = fontSizeLarge + 4
    let fullAddressHeight = fontSizeSmall + 4

    // icon
    self.contentView.addSubview(self.icon)
    self.icon.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(leftMargin)
        make.centerY.equalTo(self.snp_centerY)
        make.width.equalTo(iconHeight)
        make.height.equalTo(iconHeight)
    }
    self.icon.contentMode = UIViewContentMode.ScaleAspectFit

    // title
    self.contentView.addSubview(self.predictionTitle)
    self.predictionTitle.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(self.icon.snp_right).offset(leftMargin)
        make.top.equalTo(topMargin)
        make.right.equalTo(-rightMargin)
        make.height.equalTo(titleHeight)
    }
    self.predictionTitle.textColor = UIColor.blackColorText()
    self.predictionTitle.font = UIFont.ixiRegularFontOfSize(self.fontSizeLarge)

    // full address
    self.contentView.addSubview(self.predictionFull)
    self.predictionFull.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(self.icon.snp_right).offset(leftMargin)
        make.top.equalTo(predictionTitle.snp_bottom).offset(betweenMargin)
        make.right.equalTo(-rightMargin)
        make.height.equalTo(fullAddressHeight)
    }
    self.predictionFull.textColor = UIColor.grayColorText()
    self.predictionFull.font = UIFont.ixiRegularFontOfSize(self.fontSizeSmall)

    // border
    self.contentView.addSubview(self.borderLine)
    self.borderLine.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(leftMargin)
        make.top.equalTo(predictionFull.snp_bottom).offset(bottomMargin)
        make.right.equalTo(-rightMargin)
        make.height.equalTo(borderWidth)
    }
    self.borderLine.backgroundColor = UIColor.defaultBorderColor()
}

func setTitle(title: String?) {
    self.predictionTitle.text = ""
    if title != nil {
        self.predictionTitle.text = title
    }
}

func setAddress(address: String?) {
    self.predictionFull.text = ""
    if address != nil {
        self.predictionFull.text = address
    }
}

func setImageIcon(img: String?) {
    self.icon.image = UIImage(named: img!)
}
}

最佳答案

这实际上让我想起了我遇到的一个问题。 UITableViewCell 默认情况下有一个名为 imageView 的属性,根据您的帖子“它在初始化期间创建 imageview,并且我设置了 imageView.image '' 看来您添加了自己的 imageview ,两个变量。您是否要将图像添加到两个 ImageView 中?

您可以发布您的 AutocomplterViewCell 类来确认吗?

关于ios - UIImageView图像出现两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31609035/

相关文章:

ios - 无法从Web动态将图像设置为imageview

ios - 使用 MKNetworkKit 下载大文件会导致空文件

ios - 将 ARCharts 框架/项目导入 Swift 项目

ios - Swift 3.0 允许权限警报不间断

iphone - 当用户选择不购买 iOS InApp Purchase 中的项目时,如何自定义错误处理?

ios - Swift:在 tableView 中搜索 JSON 数据

ios - 多行自动调整大小的 UITextView

ios - 如何自定义 GKMatchmakerViewController

ios - 使用 CoreLocation 获取最近的位置

ios - 如何在表或 Collection View 中实现无限加载?