ios html 表单,iOS Form 表单

这是一个类似个人信息维护页面的列表页面。列表项左边展示标题,右边展示用户输入或选择的内容。

解决思路:

列表的cell 通用化

在VC中放置一个activeTextField,每次当用户想输入信息时,把activeTextField 放置到用户点击的那个cell 中。

在用户输入完,或选择完信息后,用一个通用的赋值函数,把值保存到用户信息model 中

演示.gif

8a50f783763a6acfd2b795e3e17c6330.gif

使用范例(可以让这个VC 更关注业务逻辑本身,只需花极少的经历到form样式和交互上):

class PersonInfoVC: BaseFormVC {

/// tableView 数据源

var datasource = [LTitleRContentModel]()

/// 个人信息,从网络获取后可以替换

var emergecyModel = PersonInfoModel()

/// 选择性别

lazy var sexAlertC = { () -> UIAlertController in

let alertC = UIAlertController(title: "selectGender", message: "", preferredStyle: .alert)

guard activeIndexpath != nil else {return alertC}

let alertAm = UIAlertAction(title: "man", style: .default) {[weak self] (action) in

guard let strongSelf = self else {return}

let cellGneder:FormTableCell = strongSelf.tableView.cellForRow(at: strongSelf.activeIndexpath!) as! FormTableCell

cellGneder.setTextFieldText(newText: action.title!)

strongSelf.configureModel(newValue: "1", forIndexpath: strongSelf.activeIndexpath!)

}

let alertAf = UIAlertAction(title: "woman", style: .default) {[weak self] (action) in

guard let strongSelf = self else {return}

let cellGneder:FormTableCell = strongSelf.tableView.cellForRow(at: strongSelf.activeIndexpath!) as! FormTableCell

cellGneder.setTextFieldText(newText: action.title!)

strongSelf.configureModel(newValue: "2", forIndexpath: strongSelf.activeIndexpath!)

}

alertC.addAction(alertAm)

alertC.addAction(alertAf)

return alertC

}()

override func viewDidLoad() {

super.viewDidLoad()

self.title = "个人信息"

}

override func confifgureSubviews() {

super.confifgureSubviews()

}

override func registCellClass() {

self.tableView.register(FormTableCell.classForCoder(), forCellReuseIdentifier: FormTableCell.cellReuseIdentifier())

}

/// table 数据配置

override func configuredatasource() {

datasource.removeAll()

let model0 = LTitleRContentModel(leftTitle: "name", rightContent: emergecyModel.truename, isNessery: true, contentType: .normalCharInput, placeHold: "placehold_cardName")

datasource.append(model0)

let model1 = LTitleRContentModel(leftTitle: "relationShip", rightContent: emergecyModel.relation_des, isNessery: true, contentType: .normalCharInput, placeHold: "PersonInfo_placehold_relation")

datasource.append(model1)

let model2 = LTitleRContentModel(leftTitle: "gender", rightContent: emergecyModel.zone_tel, isNessery: true, contentType: .selectItem, placeHold: "select gender")

datasource.append(model2)

let model3 = LTitleRContentModel(leftTitle: "phone", rightContent: emergecyModel.phone, isNessery: true, contentType: .mobileInput, placeHold: "placehold_phoneNumber")

datasource.append(model3)

let model4 = LTitleRContentModel(leftTitle: "address", rightContent: emergecyModel.proviceCityAreaString, isNessery: true, contentType: .normalCharInput, placeHold: "placehold_proviceCity")

datasource.append(model4)

let model5 = LTitleRContentModel(leftTitle: "name", rightContent: emergecyModel.truename, isNessery: true, contentType: .normalCharInput, placeHold: "placehold_cardName")

datasource.append(model5)

let model6 = LTitleRContentModel(leftTitle: "relationShip", rightContent: emergecyModel.relation_des, isNessery: true, contentType: .normalCharInput, placeHold: "PersonInfo_placehold_relation")

datasource.append(model6)

let model7 = LTitleRContentModel(leftTitle: "gender", rightContent: emergecyModel.zone_tel, isNessery: true, contentType: .selectItem, placeHold: "select gender")

datasource.append(model7)

let model8 = LTitleRContentModel(leftTitle: "phone", rightContent: emergecyModel.phone, isNessery: true, contentType: .mobileInput, placeHold: "placehold_phoneNumber")

datasource.append(model8)

let model9 = LTitleRContentModel(leftTitle: "address", rightContent: emergecyModel.proviceCityAreaString, isNessery: true, contentType: .normalCharInput, placeHold: "placehold_proviceCity")

datasource.append(model9)

self.tableView.reloadData()

}

override func numberOfSections(in tableView: UITableView) -> Int {

return 1

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return datasource.count

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let formTableCell: FormTableCell = tableView.dequeueReusableCell(withIdentifier: FormTableCell.cellReuseIdentifier(), for: indexPath) as! FormTableCell

let model: LTitleRContentModel = self.datasource[indexPath.row]

formTableCell.updateView(withModel: model)

return formTableCell

}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

activeTextField.resignFirstResponder()

activeIndexpath = indexPath

switch indexPath.row {

case 0, 1, 3, 4, 5, 6, 8, 9:

inputString()

case 2, 7:

selectItem()

default:

return

}

}

/// 用户输入

override func inputString() {

tableView.scrollToRow(at: activeIndexpath!, at: .middle, animated: true)

guard let cell: FormTableCell = tableView.cellForRow(at: activeIndexpath!) as? FormTableCell else {return}

cell.addActiveTextF(newTextF: activeTextField)

}

/// 用户选择

override func selectItem() {

self.present(sexAlertC, animated: true, completion: nil)

}

/// 选中的数据赋值

/// - Parameters:

/// - newValue: newValue description

/// - forIndexpath: forIndexpath description

override func configureModel(newValue: String?, forIndexpath: IndexPath) {

switch forIndexpath.row {

case 0, 5:

emergecyModel.truename = newValue ?? ""

case 1, 6:

emergecyModel.relation_des = newValue ?? ""

case 2, 7:

emergecyModel.gender = Int(newValue ?? "0") ?? 0

case 3, 8:

emergecyModel.phone = newValue ?? ""

case 4, 9:

emergecyModel.proviceCityAreaString = newValue ?? ""

default:

return

}

}

}

复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值