2017-03-01 37 views
0

我有表视图单元格,其中包含堆栈视图。如果某些要求是正确的,堆栈视图应该只在一个单元格中。如果不是,那么应该减少细胞的高度。 当我使用.isHidden时,高度保持不变。但我想要从该单元格中删除堆栈视图。Swift 3表视图 - 从某些单元中删除堆栈视图

这里是我的代码:

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

任何人都可以请告诉我如何做到这一点?

+0

你可以尝试加上'self.tableView.beginUpdates()'和'self.tableView.endUpdates()'AF你删除堆栈视图并计算你的新高度? – ronatory

回答

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

动态高度try代码,并给没有固定高度约束中的tableView细胞

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    return UITableViewAutomaticDimension 
} 
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    return 100.0 
} 
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 
} 
相关问题