Ios 在单元格上滚动时创建多个accessoryType的表视图
您好,我正在使用表格视图选择或取消选择单元格并获得所需结果。但当我检查任何单元格并向下滚动时,也会检查其他单元格上的显示Ios 在单元格上滚动时创建多个accessoryType的表视图,ios,swift,uitableview,Ios,Swift,Uitableview,您好,我正在使用表格视图选择或取消选择单元格并获得所需结果。但当我检查任何单元格并向下滚动时,也会检查其他单元格上的显示 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { errorText!.removeFromSuperview() tableView.deselectRowAtIndexPath(indexP
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
errorText!.removeFromSuperview()
tableView.deselectRowAtIndexPath(indexPath, animated: false)
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
let person = numberArray[indexPath.row]
if cell.accessoryType == .None {
cell.accessoryType = .Checkmark
selectedPeople.addObject(person)
}else if cell.accessoryType == .Checkmark {
cell.accessoryType = .None
selectedPeople.removeObject(person)
}
print("\(selectedPeople)")
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("commentCellss", forIndexPath: indexPath)
for object in cell.contentView.subviews
{
object.removeFromSuperview();
}
let text1:UILabel = UILabel.init(frame: CGRectMake(20, 10, 350, 30))
text1.font = UIFont.systemFontOfSize(23)
text1.text = nameArray[indexPath.row] as? String
cell.contentView.addSubview(text1)
let text2:UILabel = UILabel.init(frame: CGRectMake(20, text1.frame.origin.y+text1.frame.size.height , 350, 30 ))
text2.font = UIFont.systemFontOfSize(15)
text2.text = numberArray[indexPath.row] as? String
text2.textColor = UIColor.lightGrayColor()
cell.contentView.addSubview(text2)
let text3:UILabel = UILabel.init(frame: CGRectMake(0, text2.frame.origin.y+text2.frame.size.height+6 , 350, 1 ))
text3.font = UIFont.systemFontOfSize(15)
text3.backgroundColor = UIColor.darkGrayColor()
cell.contentView.addSubview(text3)
return cell
}
由于cellshow的可恢复性,您必须保持选择,我是否应该这样做…意味着我必须检查哪些单元格已经在索引路径的行中签入了单元格?请首先执行下面给出的答案。。。!!这对我有用请回答这个问题:--
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
errorText!.removeFromSuperview()
tableView.deselectRowAtIndexPath(indexPath, animated: false)
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
let person = numberArray[indexPath.row]
if cell.accessoryType == .None {
selectedPeople.addObject(person)
}else if cell.accessoryType == .Checkmark {
selectedPeople.removeObject(person)
}
tableView.reloadData()
print("\(selectedPeople)")
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("commentCellss", forIndexPath: indexPath)
for object in cell.contentView.subviews
{
object.removeFromSuperview();
}
let text1:UILabel = UILabel.init(frame: CGRectMake(20, 10, 350, 30))
text1.font = UIFont.systemFontOfSize(23)
text1.text = nameArray[indexPath.row] as? String
cell.contentView.addSubview(text1)
let text2:UILabel = UILabel.init(frame: CGRectMake(20, text1.frame.origin.y+text1.frame.size.height , 350, 30 ))
text2.font = UIFont.systemFontOfSize(15)
text2.text = numberArray[indexPath.row] as? String
text2.textColor = UIColor.lightGrayColor()
cell.contentView.addSubview(text2)
let text3:UILabel = UILabel.init(frame: CGRectMake(0, text2.frame.origin.y+text2.frame.size.height+6 , 350, 1 ))
text3.font = UIFont.systemFontOfSize(15)
text3.backgroundColor = UIColor.darkGrayColor()
cell.contentView.addSubview(text3)
if (selectedPeople.contains(numberArray[indexPath.row])){
cell.accessoryType = .Checkmark
}
else{
cell.accessoryType = .None
}
return cell
}