我正在尝试使用不同的单元格样式进行tableview。我的第一行图像是最大的图像,有三个标签。我只需要为第一个单元格设置这样的约束。第二行,第三行,第四行具有较小的图像和相同数量的标签。其他单元格只有标签没有图像。到目前为止,我有这段代码:
class MainPageTableViewCell: UITableViewCell {
@IBOutlet var labelDateOfPublication: UILabel!
@IBOutlet var labelTitle: UILabel!
@IBOutlet var labelIntroText: UILabel!
@IBOutlet var imageMainPage: UIImageView!
@IBOutlet var contentViewMainCell: UIView!
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
imageMainPage.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.setTranslatesAutoresizingMaskIntoConstraints(false)
var constX = NSLayoutConstraint(item: imageMainPage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
imageMainPage.addConstraint(constX)
var constY = NSLayoutConstraint(item: imageMainPage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
imageMainPage.addConstraint(constY)
var constW = NSLayoutConstraint(item: imageMainPage, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 300)
imageMainPage.addConstraint(constW)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我在这里使用这个主要单元格:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mainPage", forIndexPath: indexPath) as MainPageTableViewCell
println(indexPath.row)
cell.labelTitle.text = arrayMainPage[indexPath.row].pageTitle
cell.labelIntroText.text = arrayMainPage[indexPath.row].introText
cell.labelDateOfPublication.text = arrayMainPage[indexPath.row].pubDate
var link = arrayMainPage[indexPath.row].linkToImage as String
var imgCache = arrayMainPage[indexPath.row].imageDictionary
//var imgCache = imageDictionary
//println("print image aluew \(img?.)")
if let img = imgCache[link] {
let image = CommonFunctions.vignettePhoto(img, imageViewLocal:cell.imageMainPage)
// cell.image.image = image
println("it is not blank. index of array \(indexPath.row)")
}else{
CommonFunctions.loadImageAsync(link, imageView: cell.imageMainPage!, article: arrayMainPage[indexPath.row], viewContr: self)
println("it is blank index of array \(indexPath.row)")
}
return cell
}
但是,我添加的约束无法正常工作。我打算创建三个具有不同约束的UITableViewCell。不知何故,我需要以编程方式将细胞身份设置为我正在使用的细胞。请有人告诉我为什么我的约束不起作用?