【问题标题】:UIImageView image appearing twiceUIImageView 图像出现两次
【发布时间】:2015-07-24 11:09:13
【问题描述】:

我的tableview 单元格中有一个UIImageView,它扩展了UITableViewCell。它在初始化期间创建imageview,我在cellForRowAtIndexPath 方法中设置了imageView.image。但是由于图片尺寸小于imageView,所以图片出现了两次,如下图:

为什么会这样?我知道有一种方法可以调整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!)
}
}

【问题讨论】:

  • 添加Tableview委托方法代码。
  • 您可能还想添加setImageIcon 方法。
  • 您应该将单元格出列,而不是创建它们。
  • 我做到了,请查看编辑。它仍然无法正常工作@Mundi
  • @Justa 也添加了 setImageIcon 方法..

标签: ios objective-c swift uiimageview


【解决方案1】:

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

你能发布你的 AutocomplterViewCell 类来确认吗?

【讨论】:

  • 感谢您的回答!不,我不这样做。单元格有默认的 setImage 方法(我认为),但我没有使用它。请参阅 autocompleterviewcell 类的编辑@julian-j-tejera ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多