自定义 UITableviewCells 正确显示,但该部分中只有一个是正确的 class
Custom UITableviewCells displayed correctly but only one in the section is the correct class
对于这个问题措辞不当,我深表歉意,但我实在想不出 why/how 来简短地描述这个错误。
基本上我有一个 table 视图控制器显示两个部分,每个部分都有一个各自的自定义 UITableViewCell 子类。它们以正确的模型数据完美显示。
当我点击 "done" 按钮时出现问题,我的程序遍历所有单元格,从每个单元格收集数据并更新模型。第一部分顺利进行……但第二部分却隐藏着麻烦。此部分的第一个单元格可以转换为适当的子类,但任何其他附加单元格都不会通过此条件测试,并且它们的重用标识符为 nil。我尝试在单元格出队时检查它们,但它们 created/reused 正确。我的代码在下面,任何建议都会帮助我理智。
这是我的辅助函数中遍历单元格的部分:
for i in 0..<tableView.numberOfSections {
for j in 0...tableView.numberOfRowsInSection(i) {
let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i))
// This section seems to work fine
if(i == 0){
if let gradeCell: PercentTableViewCell = cell as? PercentTableViewCell {
print("retrieveCellInfo: gradeCell - \(gradeCell.getPercent())")
newPercentages.append(gradeCell.getPercent())
}
} else if (i == 1){
print("Else if i == 1 : j == \(j) : \(cell?.reuseIdentifier!)") // Prints "category" for the first cell and "nil" for any other
// This is the cast conditional that only lets one cell through
if let catCell = cell as? EditTableViewCell{
newCategories.append(Category(name: catCell.category!, weight: catCell.weight!, earned: catCell.earned!, total: catCell.total!))
}
}
}
}
这是我的委托函数,据我所知它工作得很好。正如我所说,UI 显示正确:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.section == 0){
/* Grade Parameter Cells */
let cell: PercentTableViewCell = tableView.dequeueReusableCellWithIdentifier("gradeParam", forIndexPath: indexPath) as! PercentTableViewCell
var gradePercentages = course?.getGradePercentages()
// Make sure percentages array is sorted correctly
gradePercentages?.sortInPlace() {
return [=12=] >
}
// Update cell's member variables
cell.initialize(letters[indexPath.row], percent: gradePercentages![indexPath.row])
return cell
} else {
/* Category Cells */
let catcell: EditTableViewCell = tableView.dequeueReusableCellWithIdentifier("category", forIndexPath: indexPath) as! EditTableViewCell
let category = categories![indexPath.row]
catcell.setLabels(category.name, earned: category.earned, total: category.total, weight: category.weight)
return catcell
}
}
The problem arises when I hit the "done" button, my program traverses
through all the cells, gathering the data from each cell and updating
the model
这是你的问题。您的单元格应该反映您的数据模型,不能依赖它们来保存数据,因为一旦单元格离开屏幕,它可能会重新用于屏幕上的行。
如果用户与单元格交互更新数据,那么您应该在他们这样做时更新您的数据模型。如果您不想立即提交更改,可以使用临时存储。
对于这个问题措辞不当,我深表歉意,但我实在想不出 why/how 来简短地描述这个错误。
基本上我有一个 table 视图控制器显示两个部分,每个部分都有一个各自的自定义 UITableViewCell 子类。它们以正确的模型数据完美显示。
当我点击 "done" 按钮时出现问题,我的程序遍历所有单元格,从每个单元格收集数据并更新模型。第一部分顺利进行……但第二部分却隐藏着麻烦。此部分的第一个单元格可以转换为适当的子类,但任何其他附加单元格都不会通过此条件测试,并且它们的重用标识符为 nil。我尝试在单元格出队时检查它们,但它们 created/reused 正确。我的代码在下面,任何建议都会帮助我理智。
这是我的辅助函数中遍历单元格的部分:
for i in 0..<tableView.numberOfSections {
for j in 0...tableView.numberOfRowsInSection(i) {
let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i))
// This section seems to work fine
if(i == 0){
if let gradeCell: PercentTableViewCell = cell as? PercentTableViewCell {
print("retrieveCellInfo: gradeCell - \(gradeCell.getPercent())")
newPercentages.append(gradeCell.getPercent())
}
} else if (i == 1){
print("Else if i == 1 : j == \(j) : \(cell?.reuseIdentifier!)") // Prints "category" for the first cell and "nil" for any other
// This is the cast conditional that only lets one cell through
if let catCell = cell as? EditTableViewCell{
newCategories.append(Category(name: catCell.category!, weight: catCell.weight!, earned: catCell.earned!, total: catCell.total!))
}
}
}
}
这是我的委托函数,据我所知它工作得很好。正如我所说,UI 显示正确:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.section == 0){
/* Grade Parameter Cells */
let cell: PercentTableViewCell = tableView.dequeueReusableCellWithIdentifier("gradeParam", forIndexPath: indexPath) as! PercentTableViewCell
var gradePercentages = course?.getGradePercentages()
// Make sure percentages array is sorted correctly
gradePercentages?.sortInPlace() {
return [=12=] >
}
// Update cell's member variables
cell.initialize(letters[indexPath.row], percent: gradePercentages![indexPath.row])
return cell
} else {
/* Category Cells */
let catcell: EditTableViewCell = tableView.dequeueReusableCellWithIdentifier("category", forIndexPath: indexPath) as! EditTableViewCell
let category = categories![indexPath.row]
catcell.setLabels(category.name, earned: category.earned, total: category.total, weight: category.weight)
return catcell
}
}
The problem arises when I hit the "done" button, my program traverses through all the cells, gathering the data from each cell and updating the model
这是你的问题。您的单元格应该反映您的数据模型,不能依赖它们来保存数据,因为一旦单元格离开屏幕,它可能会重新用于屏幕上的行。
如果用户与单元格交互更新数据,那么您应该在他们这样做时更新您的数据模型。如果您不想立即提交更改,可以使用临时存储。