Ios 如何在Swift 2.0的TableView中获取选中标记的项目
我想在我的Ios 如何在Swift 2.0的TableView中获取选中标记的项目,ios,arrays,swift,uitableview,dictionary,Ios,Arrays,Swift,Uitableview,Dictionary,我想在我的tableView中获得选中标记的项目,但我找不到任何解决方案。我试图使用NSArray,但由于'Array out bounds'错误而崩溃,这会导致崩溃,因为当我想在indepath删除时,它没有索引,所以崩溃,所以我决定使用无序集合像字典一样,但这次它不会删除所选目录中的项indepath完全正确。我的表格视图中有我的代码 let services = ["1", "2", "3"] var selectedServices = [Int: String]() var selec
tableView
中获得选中标记的项目,但我找不到任何解决方案。我试图使用NSArray
,但由于'Array out bounds'
错误而崩溃,这会导致崩溃,因为当我想在indepath
删除时,它没有索引,所以崩溃,所以我决定使用无序集合像字典
一样,但这次它不会删除所选目录中的项indepath
完全正确。我的表格视图中有我的代码
let services = ["1", "2", "3"]
var selectedServices = [Int: String]()
var selectedServicesArray = [String]()
@IBOutlet weak var saveButtonOutlet: UIButton!
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
DesignSettings().clipComponents([saveButtonOutlet])
}
// MARK: - TableView DataSource & Delegate Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return services.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let selectedRow = tableView.cellForRowAtIndexPath(indexPath) {
if selectedRow.accessoryType == .None {
selectedRow.accessoryType = .Checkmark
selectedServices[indexPath.row] = selectedRow.textLabel?.text
} else {
selectedRow.accessoryType = .None
selectedServices.removeValueForKey(indexPath.row)
}
}
for (_, value) in selectedServices {
self.selectedServicesArray.append(value)
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier(Constants.CellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = services[indexPath.row]
return cell
}
还有控制台
打印
["1", "1", "2"]
["3"]
["2", "3", "2"]
["2", "1", "1", "2", "3", "1", "2"]
我怎样才能解决这个问题?
在tableView中获取复选标记项的正确方法是什么?
谢谢 首先,我不明白你为什么需要selectedServicesArray
,你到底有什么问题。您应该始终显示您的3项服务?在您的代码中,您修改了selectedServices
和selectedServicesArray
,但在numberOfRowsInSection
中,始终是services.count
-3。所以很奇怪。更改复选标记后,您没有更新tableView。所以我提供了一些代码:
let services = ["1", "2", "3"]
var selectedServices = [Int: Bool]()
// MARK: - TableView DataSource & Delegate Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return services.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let selectedRow = tableView.cellForRowAtIndexPath(indexPath) {
if selectedRow.accessoryType == .None {
selectedRow.accessoryType = .Checkmark
selectedServices[indexPath.row] = true
} else {
selectedRow.accessoryType = .None
selectedServices[indexPath.row] = false
}
}
tableView.reloadData()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier(Constants.CellIdentifier, forIndexPath: indexPath)
// check selectedServices[indexPath.row] and update your layout
return cell
}
有两个问题:
- 您不断地将
附加到for
循环中的selectedServicesArray
。要解决此问题,您必须删除所有对象,然后添加所有选定对象
- 如果您使用中间对象来处理选定的值,我建议直接向
selectedservicesray
添加/删除这些项,如下所示:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let selectedRow = tableView.cellForRowAtIndexPath(indexPath) {
if selectedRow.accessoryType == .None {
selectedRow.accessoryType = .Checkmark
selectedServicesArray.append((selectedRow.textLabel?.text)!)
} else {
selectedRow.accessoryType = .None
let indexToDelete = selectedServicesArray.indexOf((selectedRow.textLabel?.text)!)
selectedServicesArray.removeAtIndex(indexToDelete!)
}
}
}
让indexToDelete=selectedServicesArray.indexOf((selectedRow.textLabel?.text)!)selectedServicesArray.removeAtIndex(indexToDelete!)是诀窍,谢谢。它现在工作得很好。如何将选中标记selectedServices项放在我的tableView中?没问题,但我没有完全理解您的问题,是否要在选定的单元格上添加选中标记?我将存储此数据以进行解析,当我从解析中获取此数据时,我希望显示存储的服务(selectedServices)在我的表格视图中,使用复选标记。将此项添加到您的单元格中,如果选择了ServicesArray.contains((selectedRow.textLabel?.text)!)
selectedRow.accessoryType=。复选标记否则
它可以工作,谢谢。当我想在tableView中选中另一项时,它会删除数组中的其他选中项,并仅添加新的选定项。例如:我勾选并将“1”和“2”添加到我的SelectedServicesRay中。它使用勾选标记显示此项目。当我勾选标记为“3”时,它显示3个项目,但它只将最后一个项目添加到我的selectedServicesArray,它删除最后2个项目。任何解决方案?Parse.com需要NSArray来存储此数据,因此我添加了selectedServicesArray,并希望将selectedServices附加到此数组,以便我可以将此数组存储到Parse.com。