UITableViewCell具有不同的IBOutlet布局和不同数量的IBOutlet

UITableViewCell具有不同的IBOutlet布局和不同数量的IBOutlet,uitableview,swift,layout,constraints,Uitableview,Swift,Layout,Constraints,我正在尝试使用不同的单元格样式进行tableview。我的第一行图像是最大的,有三个标签。我只需要为第一个单元格设置此类约束。第二、第三、第四行具有较小的图像和相同数量的标签。其他单元格只有标签,没有图像。到目前为止,我有以下代码: class MainPageTableViewCell: UITableViewCell { @IBOutlet var labelDateOfPublication: UILabel! @IBOutlet var labelTitle: UILabel! @

我正在尝试使用不同的单元格样式进行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。不知何故,我需要通过编程将单元格标识设置为我正在使用的单元格。请有人告诉我为什么我的约束不起作用?

如果您能正确添加约束,约束将起作用。您必须提供足够数量的约束以满足约束条件。好的,我想先尝试简单约束。那么,您认为我需要向图像和标签添加所有约束?假设您希望将图像放置在左侧并垂直居中对齐,那么您必须提供前导空间约束和垂直居中对齐约束。这两个将完美的工作形象。不工作!我将以下代码:var constLeading=NSLayoutConstraint(项:imageMainPage,属性:NSLayoutAttribute.LeadingMargin,relatedBy:NSLayoutRelation.Equal,项:contentView,属性:NSLayoutAttribute.CenterX,乘数:1,常数:0)放入imageMainPage。addConstraint(constLeading)var constY=NSLayoutConstraint(项目:imageMainPage,属性:NSLayoutAttribute.CenterY,关联者:NSLayoutRelation.Equal,toItem:contentView,属性:NSLayoutAttribute.CenterY,乘数:1,常数:0)imageMainPage.addConstraint(常量)为什么不在情节提要本身中应用这些约束。