![](/img/trans.png)
[英]Adding a UIImageView as a subview of UILabel, image not appearing
[英]UIImageView image appearing twice
我的tableview
單元格中有一個UIImageView
,它擴展了UITableViewCell
。 它創建imageview
初始化過程中,我設置imageView.image
中cellForRowAtIndexPath
方法。 但是,由於圖像的大小小於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!)
}
}
這實際上使我想起了我遇到的問題。 UITableViewCell的默認有一個叫做財產imageView
,根據您的文章“”它在初始化過程中創建的ImageView,我設置的ImageView圖像配“”好像你添加你自己的ImageView它,兩個變量。 您要將圖像添加到兩個圖像視圖中嗎?
您可以發布AutocomplterViewCell類進行確認嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.