Swift 3 Table 视图 - 从某些单元格中删除堆栈视图
Swift 3 Table View - Remove Stack View from some cells
我有 table 个视图单元格,其中包含一个堆栈视图。如果满足某些要求,堆栈视图应该只在一个单元格中。如果不是,则应降低单元格的高度。
当我使用 .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()
}
谁能告诉我怎么做?
尝试动态高度的代码,并在 tableView 单元格中给出不固定高度的约束
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 100.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
}
你应该使用不同的单元格原型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
}
我有 table 个视图单元格,其中包含一个堆栈视图。如果满足某些要求,堆栈视图应该只在一个单元格中。如果不是,则应降低单元格的高度。 当我使用 .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()
}
谁能告诉我怎么做?
尝试动态高度的代码,并在 tableView 单元格中给出不固定高度的约束
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 100.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
}
你应该使用不同的单元格原型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
}