首页 文章

Swift 3表视图 - 从某些单元格中删除堆栈视图

提问于
浏览
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()
    }

谁能告诉我怎么做?

3 回答

  • 0

    尝试动态高度的代码,并在tableView单元格中给出没有修复高度的约束

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        return UITableViewAutomaticDimension
    }
    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        return 100.0
    }
    
  • 0

    你应该使用不同的单元原型 RumCell (没有stackview)和 RumCellDetailed (带有stackview),它们都符合协议 RumCellProtocol (你可以设置 rum 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
    }
    
  • 0

    你不应该设置单元格框架 . 这不是TableView的工作方式 . 如果单元格高度是动态的,则@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
    }
    

相关问题