如何通过字符串传递先前选定的单元格。从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
}
}
答案 0 :(得分:3)
尝试以这种方式选择单元格,使用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()
}
注意:从willSelectRowAtIndexPath
移除ViewController
方法,现在不需要该方法。
答案 1 :(得分:0)
创建一个模型类并从api获取值或数组然后实现此代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell")
let relModel = bussArray.objectAtIndex(indexPath.row) as! BussinesModel
cell?.textLabel?.text = relModel.businesses_name
var imageView : UIImageView
imageView = UIImageView(frame:CGRectMake(20, 20, 15, 15))
if relModel.check
{
imageView.image = UIImage(named:"checked_checkbox")
} else{
imageView.image = UIImage(named:"checkbox_unchecked")
}
cell!.backgroundColor = UIColor.whiteColor();
cell!.tintColor = UIColor.grayColor()
cell!.accessoryView = imageView
return cell!
}