您好我正在使用表视图来选择取消选择单元格并获得所需的结果。但当我检查任何单元格并向下滚动时,也检查其他单元格上的显示
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
}
答案 0 :(得分:0)
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
}