ios - Swift 3 Table View - 从某些单元格中删除堆栈 View

标签 ios swift tableview stackview

我有表格 View 单元格,其中包含堆栈 View 等。如果满足某些要求,则堆栈 View 只能位于单元格中。如果不是,则应降低单元的高度。 当我使用 .isHidden 时,高度保持不变。但我希望从该单元格中删除堆栈 View 。

这是我的代码:

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()
    }

谁能告诉我该怎么做?

最佳答案

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

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
}

关于ios - Swift 3 Table View - 从某些单元格中删除堆栈 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42531133/

相关文章:

Iphone:拆分一个用逗号分隔的字符串并在pickerview的行中打印每个

macos - CoreData自动生成swift代码错误

ios - 是init吗?(aDecoder :) or prepareForSegue(_:sender:) called first?

objective-c - 填充 iOS TableView-EXC BAD_ACCESS 代码 = 1 错误

exception - iOS5:如何防止 rectForRowAtIndexPath 方法出现异常

ios - 大小与字体 : ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7

ios - swift : How can i obtain a documented value for UIImagePickerControllerMediaType

ios - Swift iOS - 如何将 NSTextAttachment 内容模式设置为 Aspect Fit?

swift - 在处理 API 调用时在调用中获取额外参数 'method',

ios - UITableView 自定义单元格按钮从主视图中选择其他按钮?