【发布时间】:2017-10-13 23:23:09
【问题描述】:
编辑 2:以为我拥有它,但我没有。按下按钮时我需要创建数组,但我不知道如何访问每个对象的 .checkmark 。我已经将“.selected”作为属性,但也无法访问它。
我有一个 UITableView,它在三个部分中显示一个带有复选标记的列表。 When a row in checked, the bool associated with that NSObject class changes from false to true.我在 UINavigationBar 中有一个按钮,按下该按钮会显示带有“发送”和“取消”选项的警报。当用户点击“发送”时,我希望将所有带有复选标记的行添加到数组中。我认为使用该 bool 将是最好的方法,但是在操作的 func 中,我无法调用与数组中每个 NSObject 关联的 bool。我包括了我认为需要帮助我的代码部分。
编辑:我相信这就是我设置班级的方式,除非我有误解。类的变量之一是“var selected: Bool”,然后当我从该类创建一个项目时,我将其设置为 false。我有一个在 cellForRowAt 中显示良好的项目列表,但是当点击按钮时,我无法访问相同的“sortedList”。这里有更多代码。也许我还缺少其他东西?
var arrayOfItemsSelected = [String]()
var sortedItems = [[Item]]()
var itemList: ItemList! {
didSet {
sortedItems = itemList.sortByBrands()
self.tableView.reloadData()
}
}
覆盖 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 让 item = sortedItems[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "UIItemViewCell", for: indexPath)
cell.textLabel?.text = "\(item.name)"
cell.detailTextLabel?.text = "\(item.style)"
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none // to prevent cells from being "highlighted"
return cell
}
func confirmButtonPressedAction() {
// Create the alert controller
let alertController = UIAlertController(title: "List of checked items", message: stringOfSelectedItems, preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.default) {
UIAlertAction in
self.arrayOfItemsSelected.removeAll()
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
【问题讨论】:
标签: ios arrays swift uitableview swift3