在我的Android应用程序中,我可以使用标准的表格创建行,并在其中包含文本框。根据用户首选项,这些行中的一部分会显示/隐藏。例如,如果用户不想输入现金/信用销售,或跟踪给予客户的任何折扣等,这些字段不应该出现。
我只是简单地隐藏了整个行,就像html中的DIV一样,其余的行会很好地向上移动。
举个例子:
element1
element2
element3
我想去掉元素2,3向上移动,取代了2的位置。顺便说一句,我可能会有8个这样的字段。
然而,在iOS中,这样的事情是可能的吗?一些谷歌搜索的结果或多或少地使得显示/隐藏表单元素和让一切顺利进行变得非常复杂。一些解决方案包括动态设置非常复杂的自动布局场景,或者根本不使用自动布局等等。不过,这些解决方案适用于旧版ios。
这样的事情有可能发生吗?我怎样才能做到这一点呢?
谢谢!
发布于 2014-10-19 01:38:03
使用 。
UITableView允许您表示单元格列表,并使用动画对其进行操作(添加、删除、重新排序)。
在这种情况下,每个选项都将是一个UITableViewCell。
Set
的子类Set a dataSource of UITableView
这就是2个函数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell 通过编辑对象并调用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()中移除或添加特定项目
首先通过删除或添加对象来更新对象,然后调用更新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.
}
}https://stackoverflow.com/questions/26442515
复制相似问题