swift - 为重复数据和无数据创建一个页面
swift - create a page for both repeated data and for no data
我需要有关 ViewContoller 方案的建议。我应该创建一个包含帐单地址的视图。可以根本没有地址,也可以有一些地址。如果没有,应该只有一个按钮"Add New"。如果有地址,每个地址也应该有编辑、删除和 "Add New" 按钮。
我有这个 VC 的数据 JSON,已解析并保存到 plist。
那么让这个视图看起来不同的逻辑是什么取决于 1) 是否有地址?和 2) 如果有 1 个、2 个或 20 个账单地址?
非常感谢!
我用 UITableVIew、UITableViewDataSource 和 UITableViewDelegate 解决了这样的问题:
为一个部分(地址)设置 table 视图
func numberOfSections(in tableView: UITableView) -> Int {return 1;}
return委托方法中的地址数组长度
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return adresses.count
}
如果数组长度为 0,则设置页脚视图
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if adresses.count == 0 {
let vw = YourViewClass()
//I use blockskit library here (vw.bk_) to recognize a tap, but you can add a button by yourself
vw.bk_(whenTapped: {
//Create and present your next viewcontroller to
})
return vw
}
return nil
}
如果有地址,将页脚高度设置为 0
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if addresses.count > 0 {
return YOUR_DESIRED_FOOTER_HEIGHT_FOR_INPUT
}
return 0
}
为每个地址创建行
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let address = addresses[indexPath.row]
let tableViewCell = UITableViewCell() //maybe you have to create your own if the layout does not fit
//set tableViewCell's title / description to show address values
return tableViewCell
}
在这种情况下,当没有可用地址时,会显示带有添加按钮的页脚视图(如果需要,您可以在页眉中执行相同的操作),而当地址可用时,它会隐藏。
我需要有关 ViewContoller 方案的建议。我应该创建一个包含帐单地址的视图。可以根本没有地址,也可以有一些地址。如果没有,应该只有一个按钮"Add New"。如果有地址,每个地址也应该有编辑、删除和 "Add New" 按钮。
我有这个 VC 的数据 JSON,已解析并保存到 plist。
那么让这个视图看起来不同的逻辑是什么取决于 1) 是否有地址?和 2) 如果有 1 个、2 个或 20 个账单地址?
非常感谢!
我用 UITableVIew、UITableViewDataSource 和 UITableViewDelegate 解决了这样的问题:
为一个部分(地址)设置 table 视图
func numberOfSections(in tableView: UITableView) -> Int {return 1;}
return委托方法中的地址数组长度
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { return adresses.count }
如果数组长度为 0,则设置页脚视图
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { if adresses.count == 0 { let vw = YourViewClass() //I use blockskit library here (vw.bk_) to recognize a tap, but you can add a button by yourself vw.bk_(whenTapped: { //Create and present your next viewcontroller to }) return vw } return nil }
如果有地址,将页脚高度设置为 0
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { if addresses.count > 0 { return YOUR_DESIRED_FOOTER_HEIGHT_FOR_INPUT } return 0 }
为每个地址创建行
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let address = addresses[indexPath.row] let tableViewCell = UITableViewCell() //maybe you have to create your own if the layout does not fit //set tableViewCell's title / description to show address values return tableViewCell }
在这种情况下,当没有可用地址时,会显示带有添加按钮的页脚视图(如果需要,您可以在页眉中执行相同的操作),而当地址可用时,它会隐藏。