【发布时间】:2018-07-05 06:05:10
【问题描述】:
我正在向左滑动以删除 Swift 3 中的自定义视图单元格。
单元格是:
class CustomTableCell: SwipeTableViewCell
{
public var atest = UILabel();
public var btest = UILabel();
var animator: Any?
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
super.init(style: style, reuseIdentifier: reuseIdentifier);
let height = 140;
atest = UILabel(frame: CGRect(x: 20 + (activityWidth / 2), y: 72, width: (activityWidth / 2) - 10, height: 30));
btest = UILabel(frame: CGRect(x: 20 + (activityWidth / 2), y: 102, width: (activityWidth / 2) - 10, height: 30));
self.contentView.addSubview(atest);
self.contentView.addSubview(btest);
}
然后在我的表格视图控制器中,我有:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! CustomTableCell;
cell.atest.text = "text from an array at indexpath.row";
cell.btest.text = "another different text from an array";
return cell;
}
表格视图控制器中的删除发生在这里:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]?
{
let delete = SwipeAction(style: .destructive, title: "Delete")
{
action, indexPath in
print("delete button tapped");
// self.table.deleteRows(at: [indexPath], with: .none);
// database is a string index array
self.database.remove(at: indexPath.row);
if (self.database.count == 0)
{
self.noText.isHidden = false;
self.footer.isHidden = false;
self.table.tableFooterView = self.footer;
}
// self.table.setNeedsDisplay();
}
delete.backgroundColor = UIColor.red;
return [delete];
我通过向左滑动进行删除,它会正确删除所有内容。我遇到的问题是删除使折叠下的所有表格视图单元格与最后一个可见单元格相同。
我该如何解决这个问题?我也在使用自定义单元格视图。
一个例子是我有 6 行,前 4 行是可见的。删除第一行使第 4 行和第 5 行相同。如在最后一个可见行也成为第一个不可见行。 prepareForReuse 可能无法正常工作。
删除有效,从 6 行变为 5 行,但下面是一个示例。
UITableView
First row label A
Second row label B
Third row label C
Fourth row label D (last visible row)
Fifth row label E (first non visible row)
Sixth row label F
通过滑动删除第一行会创建这个新的 UITableView:
UITableView
Second row label B
Third row label C
Fourth row label D
Fourth row label D (last visible row)
Fifth row label E (first non visible row)
可重复使用的单元格无法正常工作。
我不使用 awakeFromNib,也刚升级到 swift 4.1。
【问题讨论】:
-
使用处理删除的代码更新您的问题。
-
我更新了删除,用github.com/SwipeCellKit/SwipeCellKit处理刷屏
-
不相关但你为什么不在Interface Builder中设计单元格?这就是 Swift:
if条件周围没有尾随分号和括号。 -
当您删除/移除单元格时,自定义单元格视图会随之消失。 我遇到的问题是删除使折叠下的所有表格视图单元格与最后一个可见单元格相同是什么意思?可以分享截图吗?
-
它们是屏幕下方的行,直到滚动到它们才能看到
标签: ios iphone swift uitableview swift4