我想要一个有 2 个单元格的表格 View 。
一种带有增加高度的标签,一种没有该标签。用户将内容添加到此表格 View (有点像待办事项列表),如果用户选择一个选项,则标签在他们添加的新项目的表格 View 单元格中可见,但如果他们不选择该选项,那么他们添加一个没有该标签的 tableviewcell。无论如何,我是否可以选择何时显示带有标签的单元格。
我首先想到的是,如果未选择该选项,则使标签不可见,但这会影响其他标签的高度和约束。
是否可以创建 2 个具有不同标识符的不同单元格,并根据是否选择该选项来调用要添加的单元格。
根据解决方案,我尝试在两个不同的单元之间切换,这是我这样做的代码。
if newMode==true{
let cell = tableView.dequeueReusableCell(withIdentifier: "monthlyCell", for: indexPath) as! MonthlyExpenseTableViewCell
let screenSize: CGRect = UIScreen.main.bounds
cell.dateLabelBackground.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: 31)
cell.dateLabel.text = dateString
cell.expenseName2.text = expense.name
cell.expenseAmount2.text = finalDisplayed
cell.expenseCategory2.text = expense.category
cell.expenseCollection2.text = expense.collection
if (expense.expense) {
cell.expenseAmount2.textColor = UIColor.red
}
else if (expense.income){
cell.expenseAmount2.textColor = UIColor.green
}
if (expense.cash) && (expense.expense){
cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Expense Icon")
}
else if (expense.cash) && (expense.income){
cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Income Icon")
}
else if (expense.credit) && (expense.income){
cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Income Icon")
}
else if (expense.credit) && (expense.income){
cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Expense Icon")
}
}
else if newMode==false{
let cell = tableView.dequeueReusableCell(withIdentifier: "monthlyCell2", for: indexPath) as! MonthlyExpenseTableViewCellShort
cell.expenseName3.text = expense.name
cell.expenseAmount3.text = finalDisplayed
cell.expenseCategory3.text = expense.category
cell.expenseCollection3.text = expense.collection
if (expense.expense) {
cell.expenseAmount3.textColor = UIColor.red
}
else if (expense.income){
cell.expenseAmount3.textColor = UIColor(red:0.49, green:0.83, blue:0.13, alpha:1.0)
}
if (expense.cash) && (expense.expense){
cell.cashOrCredit3.image = #imageLiteral(resourceName: "Cash-Expense Icon")
}
else if (expense.cash) && (expense.income){
cell.cashOrCredit3.image = #imageLiteral(resourceName: "Cash-Income Icon")
}
else if (expense.credit) && (expense.income){
cell.cashOrCredit3.image = #imageLiteral(resourceName: "Credit-Income Icon")
}
else if (expense.credit) && (expense.income){
cell.cashOrCredit3.image = #imageLiteral(resourceName: "Credit-Expense Icon")
}
}
return cell
}
问题在于,由于某种原因,它一次只能返回单元格。当我删除单元格时,它会显示下一个单元格。
这是我设置 bool 值“newMode”的代码
if indexPath.row == 0{
newMode = true
}
if indexPath.row>1{
let previousExpensesData = monthlyExpenses[indexPath.row - 1].modificationDate
let day = Calendar.current.component(.day, from: expense.modificationDate as! Date) // Do not add above 'date' value here, you might get some garbage value. I know the code is redundant. You can adjust that.
let previousDay = Calendar.current.component(.day, from: monthlyExpenses[indexPath.row - 1].modificationDate! as Date)
if day == previousDay {
newMode = false
} else {
newMode = true
}
}
对于我的 heightForRowAtIndexPath,我使用此代码
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat
{
if newMode==true {
return 95
}
else if newMode==false {
return 64
}
else {
return 0
}
}
这是 numberOfRows 的代码
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if monthlyExpenses.count == 0{
noExpenseLabel.isHidden = false
}
return monthlyExpenses.count
}
这是 2 个单元的外观
最佳答案
实际上您不需要有两个不同的单元。正如您刚才在问题中所说的“如果未选择该选项,我首先想到的是使标签不可见,但这会影响其他标签的高度和约束。”
只需使用一个单元格。
现在要克服这个问题,请使用 UIStackView。将该标签放入此 stackView 中。对stackView给予适当的约束。并根据您的选择隐藏/取消隐藏标签。 UIStackView 会为你做这些事情。 :)
它会给你你想要的结果。有关 UIStackView 的更多信息,请阅读 this awesome tutorial作者:雷文德利希。
编辑:根据您的评论
我创建了一个像这样的单元格..
我像这样管理该单元格的所有 subview ..
我刚刚在cellForRow
中写了一行代码
cell.label.isHidden = indexPath.row % 2 == 1
我得到的输出是这样的......
关于ios - 切换单元格的UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902649/