0

我有表格视图单元格,其中包含堆栈视图等。如果某些要求是真实的,堆栈视图应该只在一个单元格中。如果不是,则应降低单元格的高度。当我使用 .isHidden 时,高度保持不变。但我希望从该单元格中删除堆栈视图。

这是我的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "RumCell", for: indexPath) as! RumCell

    let currentRum: Rum
    currentRum = rumList[indexPath.row]

    cell.rum = currentRum

    if (cell.rum?.clubRatingJuicy == 0) && (cell.rum?.clubRatingGasy == 0) && (cell.rum?.clubRatingSpicy == 0) && (cell.rum?.clubRatingSweet == 0) {
        cell.frame.size.height -= 76
    }

    return cell
}

如您所见,我尝试降低单元格高度,但这不起作用。我也试过这个,它不起作用:

    if (cell.rum?.clubRatingJuicy == 0) && (cell.rum?.clubRatingGasy == 0) && (cell.rum?.clubRatingSpicy == 0) && (cell.rum?.clubRatingSweet == 0) {
        cell.tastStack.removeFromSuperview()
    }

谁能告诉我该怎么做?

4

3 回答 3

0

尝试动态高度的代码,并在 tableView 单元格中给出不固定高度的约束

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return 100.0
}
于 2017-03-01T12:18:16.250 回答
0

您不应该设置单元格框架。这不是 TableViews 的工作方式。如果单元格高度是动态的,那么@Theorist 是正确的。如果没有,您可以实施

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath:     NSIndexPath) -> CGFloat
{
    if let cell = tableView.cellForRowAtIndexPath(indexPath), let rum  = cell.rum, rum.clubRatingJuicy == 0 && rum.clubRatingGasy == 0 && rum.clubRatingSpicy == 0 && rum.clubRatingSweet == 0 {
    return {no stackview height} //whatever the height should be for no stackview
}
    return {normal height} //whatever your value is
} 
于 2017-03-01T14:23:35.850 回答
0

您应该使用不同的单元原型RumCell(不带stackview)和RumCellDetailed(带stackview),它们都符合协议RumCellProtocol(您可以在其中设置rumvar)

protocol RumCellProtocol {
    func config(rum: Rum)
}

这个代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cellIdentifier = "RumCellDetailed"

    if (cell.rum?.clubRatingJuicy == 0) && (cell.rum?.clubRatingGasy == 0) && (cell.rum?.clubRatingSpicy == 0) && (cell.rum?.clubRatingSweet == 0) {
        cellIdentifier = "RumCell"
    }


    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RumCellProtocol

    let currentRum: Rum
    currentRum = rumList[indexPath.row]

    cell.config(rum: currentRum)

    return cell
}
于 2017-03-03T01:44:41.327 回答