gpt4 book ai didi

swift - 隐藏一个 View 后向上移动所有 View

转载 作者:行者123 更新时间:2023-11-28 07:15:29 24 4
gpt4 key购买 nike

在我的 Android 应用程序中,我能够创建一个包含行并在其中包含文本框的标准表格。根据用户偏好,显示/隐藏其中一些行。例如,如果用户不想输入现金/信贷销售,或跟踪给予客户的任何折扣等,则不应显示这些字段。

我会简单地隐藏整行,就像 html 中的 DIV 一样,其余的行会很好地向上移动。

例如:

元素1
元素2
元素3

我想删除元素 2,然后 3 向上移动并取代 2。顺便说一句,我可能会有 8 个这样的字段。

然而,在 iOS 中,这样的事情可能吗?一些谷歌搜索产生的结果或多或少使显示/隐藏表单元素以及让所有内容都很好地向上滑动看起来非常复杂。一些解决方案包括动态设置非常复杂的自动布局方案,或者根本不使用自动布局等。再一次,这些解决方案适用于旧版本的 ios。

这样的事情可能吗?我怎样才能做到这一点?

谢谢!

最佳答案

使用 UITableView .
UITableView 允许您表示单元格列表并使用动画操作它们(添加、删除、重新排序)。
在你的情况下,每个选项都是一个 UITableViewCell。

  1. 创建 UITableViewController 的子类
  2. 设置 UITableView 的数据源
  3. 实现 UITableViewDataSource

那是2个函数

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell
  1. 更新 TableView 项目

您可以通过编辑对象并在 tableView 上调用 reloadData 方法轻松地重新加载 tableView

// Add, remove or replace your objects you want to display in table View
objects = ["1", "2", "3"]
// call tableView to reload it's data. TableView will call dataSource delegates methods and update itself
tableView.reloadData()
  1. 您可以在 tableView 中删除或添加特定项目

首先更新您的对象,通过删除或添加它,而不是调用更新 tableView 并说明它应该添加或删除哪个单元格(在哪个位置)。你也可以说它应该使用什么类型的动画

var index = 2
// Update your object Model, here we remove 1 item at index 2
objects.removeAtIndex(index)
// saying tableView to remove 1 cell with Fade animation.
tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: .Fade)

完整代码示例

class MasterViewController: UITableViewController {

var objects = ["Option 1", "Option 2", "Option 3"]

func insert(obj: String, at: Int) {
objects.append(obj)
let indexPath = NSIndexPath(forRow: at, inSection: 0)
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}

func deleteAt(index: Int) {
objects.removeAtIndex(index)
tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: .Fade)
}


override func viewDidLoad() {
super.viewDidLoad()
}

func insertNewObject(sender: AnyObject) {
insert(NSDate.date().description, at: 0)
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let object = objects[indexPath.row]
cell.textLabel?.text = object
return cell
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}


override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
deleteAt(indexPath.row)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}

关于swift - 隐藏一个 View 后向上移动所有 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26442515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com