我正在使用 Eureka使用 Swift 在 iOS 中构建表单。我创建了一个多值部分,例如:
form +++ MultivaluedSection(multivaluedOptions: [.Insert, .Delete], header: "My Header", footer: "My footer") { section in
section.tag = "mySectionTag"
section.addButtonProvider = { _ in
return ButtonRow() { row in
row.title = "Add row"
}
}
section.multivaluedRowToInsertAt = { index in
return TimeInlineRow() { row in
row.title = "My row title"
}
}
// initialize form with any times that have already been set previously, times: [Date]
for time in times {
section <<< TimeInlineRow(tag) { row in
row.value = time
row.title = "My row title"
}
}
我想限制您可以插入到我的多值部分的行数。正在考虑通过使用某种 Condition
隐藏 ButtonRow
来做到这一点,但我不确定如何连接它。或者,当该部分中 values()
的计数太高时,如果您点击按钮行,则可以只显示警报,但是您如何阻止实际插入?
也在考虑我可以根据索引在 multivaluedRowToInsertAt
中做一些事情,但仍然不确定是什么。
浏览了这些问题,很惊讶没有找到任何关于这个的东西,所以我只能假设我遗漏了一些明显的东西。
我的另一个想法是在 addButtonProvider
中的 ButtonRow
上设置一个 Condition
,如果具有特定最大行的行返回 true标签(我创建的)在表单中不是零(即不存在这样的行),然后在 multivaluedRowToInsertAt
中它会检查索引是否大于允许的最大索引,如果是这样它适用创建该行时的最大标签。但似乎无论类型如何,绿色 + 插入按钮都会自动应用于该部分的最后一行。然后我尝试在达到最大行数时将 multivaluedOptions
更改为 .Delete
但我无法弄清楚如何让它返回到允许在行被删除。
还尝试根据与上述类似的方法(使用最大行)在 ButtonRow
的禁用属性上设置条件,但它也会遇到重复的行标记问题并且绿色添加按钮仍然存在响应点击,showInsertIconInAddButton
属性无效。
即使我让这个方法起作用,它似乎也不必要地令人费解,我希望有一个更简单的解决方案,因为这似乎是很多人需要的那种功能。
最佳答案
如 Mahbub's answer 中所述并在原始问题中暗示,可以检查 multivaluedRowToInsertAt
block 中的索引并更新 multivaluedOptions
并相应地隐藏按钮行。
FormViewController
中的属性:
private var myButtonRow: ButtonRow! // Can also just refer to it by tag
let kMaxCount = 5
在 FormViewController
中的设置函数中:(未显示,设置部分/按钮行/添加提供程序等)
section.multivaluedRowToInsertAt = { index in
if index >= self.kMaxCount - 1 {
section.multivaluedOptions = [.Delete]
self.myButtonRow.hidden = true
DispatchQueue.main.async() { // I'm not sure why this is necessary
self.myButtonRow.evaluateHidden()
}
}
return TimeRow() { row in // any row type you want — although inline rows probably mess this up
row.title = title
row.value = Date()
}
}
在添加第 6 行之前,multivaluedRowToInsertAt
中按钮行的更改似乎没有生效,无论何时调用隐藏方法以及最大计数设置为多少,并且插入的最后一行排在倒数第二位。然后我尝试了上面写的代码,调度调用延迟 evaluateHidden()
并且它似乎工作。我不确定为什么,大概是一些相互冲突的竞争条件。请注意,调用插入方法时它是在主线程上调用的,因此它与在后台线程上更改 UI 无关。
然后,当行被删除时,有一个名为 rowsHaveBeenRemoved
的函数,您可以在 FormViewController
子类中重写该函数,每当一行(在任何部分中)被删除时调用该函数:
override func rowsHaveBeenRemoved(_ rows: [BaseRow], at indexes: [IndexPath]) {
super.rowsHaveBeenRemoved(rows, at: indexes)
if let index = indexes.first?.section, let section = form.allSections[index] as? MultivaluedSection {
if section.count < kMaxCount {
section.multivaluedOptions = [.Insert, .Delete]
myButtonRow.hidden = false // or could
myButtonRow.evaluateHidden()
}
}
}
关于ios - Swift Eureka 表单 : how do you limit the number of rows in a multivalued section?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47932337/