Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 选择一行后,将自动从UITableView中选择另一行_Ios_Xcode_Swift_Uitableview - Fatal编程技术网

Ios 选择一行后,将自动从UITableView中选择另一行

Ios 选择一行后,将自动从UITableView中选择另一行,ios,xcode,swift,uitableview,Ios,Xcode,Swift,Uitableview,我正在开发一个具有表视图的应用程序, 每个表视图单元格仅包含标签, 我已将电池附件类型设置为选中标记, 如果我选择了一行,复选标记就会出现 一切都在进行中 但当我选择一行并向下滚动表格视图时,其他行上会出现复选标记, 所以请帮帮我 我已附上下面的代码 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(

我正在开发一个具有表视图的应用程序, 每个表视图单元格仅包含标签, 我已将电池附件类型设置为选中标记, 如果我选择了一行,复选标记就会出现 一切都在进行中

但当我选择一行并向下滚动表格视图时,其他行上会出现复选标记, 所以请帮帮我

我已附上下面的代码

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  tableView.deselectRowAtIndexPath(indexPath, animated: true)
  let selectRow = self.MytableView.cellForRowAtIndexPath(indexPath)!
  var can: Candy
  if tableView == self.searchDisplayController?.searchResultsTableView{
     can = self.filteredCandy[indexPath.row]
  }else{
     can = self.candies[indexPath.row]
  }
let name = "\(can.firstName) \(can.lastName)"
 if selectRow.accessoryType == UITableViewCellAccessoryType.None{
    selectRow.accessoryType = UITableViewCellAccessoryType.Checkmark
    selectRow.tintColor = UIColor.blackColor()
    DataStorageManager.sharedInstance.guests.append(name)
 }else{
    selectRow.accessoryType = UITableViewCellAccessoryType.None
    DataStorageManager.sharedInstance.guests == DataStorageManager.sharedInstance.guests.filter(){$0 != name}   
    }
}

您在逻辑上犯了一个错误,即您将对新单元格进行选中标记,但未对最后选定的行进行取消选中标记。所以请实现我的逻辑。 首先,创建一个全局变量,如

 let selectedRow = 0

And then implement this one 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let selectedIndexPath = NSIndexPath.init(forItem: selectedRow, inSection: 0)
        let selectedCell = tableView.cellForRowAtIndexPath(selectedIndexPath)! as UITableViewCell

        selectedCell.accessoryType = .None

let selectRow = self.MytableView.cellForRowAtIndexPath(indexPath)!
  var can: Candy
  if tableView == self.searchDisplayController?.searchResultsTableView{
     can = self.filteredCandy[indexPath.row]
  }else{
     can = self.candies[indexPath.row]
  }
let name = "\(can.firstName) \(can.lastName)"
 if selectRow.accessoryType == UITableViewCellAccessoryType.None{
    selectRow.accessoryType = UITableViewCellAccessoryType.Checkmark
    selectRow.tintColor = UIColor.blackColor()
    DataStorageManager.sharedInstance.guests.append(name)
 }else{
    selectRow.accessoryType = UITableViewCellAccessoryType.None
    DataStorageManager.sharedInstance.guests == DataStorageManager.sharedInstance.guests.filter(){$0 != name}   
    }

selectedRow = indexPath.row
}

我通过向UITableView的CellForRowatineXpath方法中添加以下代码来解决上述问题

if tableView == self.searchDisplayController?.searchResultsTableView{
can = self.filteredCandy[indexPath.row]
}else{
can = self.candies[indexPath.row]
}

在这里,我解决的大多数TableViewCell问题,如在滚动后单击单元格按钮时,可能会更改其余的行;在didSelectRowAt中选择indexPath:indexPath时,可能会选择滚动后的行。所以我创建了一个应用程序来解决这些问题。这里添加GitHub代码

  • 在第一个屏幕中,单击红色按钮并向下滚动,在UITableViewCell中看不到其他单元格自动受到影响

  • 单击第一个屏幕中的didSelectRowAt indexPath:indexPath,可以转到第二个屏幕的详细信息屏幕,在该屏幕中,选择一行后,UITableViewCell中不会自动选择其他行


请为rowatindexpath方法添加代码cellForRowAtIndexPath。但我希望它用于多选,我只会让您知道登录,将所有选定行的索引存储在数组中,并在cellforrowatindexapath方法中检查它是否存在于数组中,然后将其选中,否则将其选中。None@Dharmir Singh请给出示例,如果您have@RaviG不,我没有,但我可以帮助您向我展示您的codefunc tableView(tableView:UITableView,numberOfRowsInSection:Int)->Int{var a:Int!if tableView==self.searchDisplayController?.searchResultsTableView{a=self.filteredCandy.count}否则{a=self.candies.count}返回a}