UIImageView图像出现两次

时间:2015-07-24 11:09:14

标签: ios objective-c swift uiimageview

我的UIImageView单元格中有tableviewUITableViewCell。它在初始化期间创建imageview,并在imageView.image方法中设置cellForRowAtIndexPath。但是,由于图片尺寸小于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!)
}
}

1 个答案:

答案 0 :(得分:0)

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

您可以发布AutocomplterViewCell类进行确认吗?