gpt4 book ai didi

ios - 根据 API 的结果选择 Tableview 单元格

转载 作者:可可西里 更新时间:2023-11-01 00:58:56 25 4
gpt4 key购买 nike

如何通过字符串传递上一个选定的单元格。从 API 我通过“|”得到结果分开,然后我在删除分隔符后将其放回 array 中。但是如何将结果传递到 tableView 中,它将根据结果选择 tableview 单元格。

这是我如何进行选择并将结果保存到数组中的虚拟代码。

class TableViewController: UITableViewController
{
let limit = 5
var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0)
var genreList = ["A","B","C","D","E","F","G","H"]
var stringRepresentation = String()
var result = [String]()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)

// Configure the cell...
cell.textLabel!.text = genreList[indexPath.row]

if cell.selected
{
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else
{
cell.accessoryType = UITableViewCellAccessoryType.None
}
return cell
}


override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
if let sr = tableView.indexPathsForSelectedRows {
if sr.count == limit {
let alertController = UIAlertController(title: "Oops", message:
"You are limited to \(limit) selections", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: {action in
}))
self.presentViewController(alertController, animated: true, completion: nil)

return nil
}

}

return indexPath
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
print("selected \(genreList[indexPath.row])")

let cell = tableView.cellForRowAtIndexPath(indexPath)

if cell!.selected == true
{
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
result.append((cell?.textLabel?.text)!)
print(result)
}
else
{

cell!.accessoryType = UITableViewCellAccessoryType.None
print(result)
}
}


override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

let deselectedCell = tableView.cellForRowAtIndexPath(indexPath)

result = result.filter { $0 != deselectedCell?.textLabel?.text }

stringRepresentation = result.joinWithSeparator("|") // "1-2-3"

print(stringRepresentation)

deselectedCell!.accessoryType = UITableViewCellAccessoryType.None

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return genreList.count
}

}

最佳答案

尝试像这样选择单元格,为此使用您的result数组

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)

// Configure the cell...
cell.textLabel!.text = genreList[indexPath.row]
if result.contains(genreList[indexPath.row])
{
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else
{
cell.accessoryType = UITableViewCellAccessoryType.None
}
return cell
}

现在像这样改变didSelectRowAtIndexPath

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if (self.result.count != limit) {
if (self.result.contains(genreList[indexPath.row])) {
let index = self.result.indexOf(genreList[indexPath.row])
self.result.removeAtIndex(index)
}
else {
self.result.append(genreList[indexPath.row])
}
}
else {
if (self.result.contains(genreList[indexPath.row])) {
let index = self.result.indexOf(genreList[indexPath.row])
self.result.removeAtIndex(index)
}
else {
let alertController = UIAlertController(title: "Oops", message:
"You are limited to \(limit) selections", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: {action in
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
stringRepresentation = result.joinWithSeparator("|")
self.tableView.reloadData()
}

注意:ViewController 中删除 willSelectRowAtIndexPath 方法,现在不需要该方法了。

关于ios - 根据 API 的结果选择 Tableview 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38759002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com