2016-07-08 62 views
0

我有一个tableViewCell,它在单元格的accessoryType中使用复选标记。我有一个函数将单元格的内容放入textField中,并在未选中时从文本字段中删除文本。TableView选中的单元格 - 选中的单元格在滚动不见时会消失并检查新的单元格

它似乎工作正常,但如果我检查一个单元格,并希望检查一个单元格不可见(IOW)我需要滚动tableView,被检查的单元格(现在不可见)似乎取消选中它自己(只有当我检查一个新的可见细胞)。

多重选择仅适用于可见细胞。

这里是我的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) 
    let row = indexPath.row 
    cell.textLabel?.text = painArea[row] 
    cell.accessoryType = .None 
    return cell 
    } 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //selectedRow = indexPath 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    let row = indexPath.row 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
     if cell!.accessoryType == .Checkmark { 
      cell!.accessoryType = .None 
     } else { 
      cell!.accessoryType = .Checkmark 
     } 
    populateDescription() 
    print(painArea[row]) 
} 

var painDescription = ["very sore"] 

func populateDescription() { 
    painDescription.removeAll() 
    severityText.text = "" 
    for cell in tableView.visibleCells { 
     if cell.accessoryType == .Checkmark { 
      painDescription.append((cell.textLabel?.text)! + " ") 
     } 
     var painArea = "" 
     var i = 1 

     while i <= painDescription.count { 
      painArea += painDescription[i-1] + "~" 
      i = i + 1 
     } 
     severityText.text = painArea 

    } 

我希望我充分解释自己。我不希望不可见的单元格被取消选中,因此从我的文本字段中删除,除非我取消选中它。

任何想法将不胜感激。

亲切的问候

韦恩

+0

请发表您的cellForRowAtIndexPath方法.. –

+0

FUNC的tableView(的tableView:UITableView的,的cellForRowAtIndexPath indexPath:NSIndexPath) - > {UITableViewCell中让 细胞= tableView.dequeueReusableCellWithIdentifier(textCellIdentifier,forIndexPath:indexPath) 令行= indexPath.row 细胞.textLabel?.text = painArea [row] cell.accessoryType =。无 返回单元格 } – Wayne

+0

在您的cellForRowAtIndexPath方法中添加if-else条件以检查单元格是否具有复选标记。如果是,那么添加accesoryType .checkmark否则.none .. –

回答

1

由于Cell的可重用性,而不是在didSelect中设置Checkmark尝试在cellForRowAtIndexPath中设置。还需要创建model类这样的解决你的问题

class ModelClass: NSObject { 
    var isSelected: Bool = false 
    //Declare other property that you are using cellForRowAtIndexPath 
} 

现在cellForRowAtIndexPath检查这isSelected像下面。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) 
    let row = indexPath.row 
    let modelClass = painArea[row] 
    cell.textLabel?.text = modelClass.name 
    if modelClass.isSelected { 
     cell.accessoryType = .Checkmark 
    } 
    else { 
     cell.accessoryType = .None 
    } 
    return cell 
} 

现在改变你的didSelect这样

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    var modelClass = painArea[indexPath.row] 
    modelClass.isSelected = !modelClass.isSelected 
    self.tableView.reloadData() 
    populateDescription() 
    print(painArea[row]) 
} 

希望这会帮助你。

0

这是因为每当ü滚动细胞出来的观点和向后滚动,它会再次致电cellForRow并设置一切恢复默认情况下,你所要做的就是创建一个什么正确地dataSource,每当一个单元格被检查,你用Bool更新数据源表明它已被检查,然后将其设置回cellForRowcellWillDisplay

+0

在我的cellForRowAtIndexPath中我有:accesoryType as .None。我已经评论了这一点,它似乎工作。 @ Dev.RK通过要求我发布我的cellForRowAtIndexPath方法帮助我 – Wayne

+0

这种做法仅用于保持用户界面,您应更新您的数据源,而不是用户界面本身,Nirav的回答显示了基本的做法 – Tj3n

相关问题