2016-07-08 1 views
0

セルのaccessoryTypeにチェックマークを付けるtableViewCellがあります。私は、セルの内容を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) LET行= indexPath.row セル.textLabel?.text = painArea [行] cell.accessoryType = .None 戻りセル } – Wayne

+0

セルをチェックするためのcellForRowAtIndexPathメソッドのif-else条件にチェックマークが付いているかどうかを指定します。 Yesの場合は、accesoryType .checkmarkを追加します。それ以外の場合は.none .. –

答えて

1

に再設定します。これはCellの再利用可能性のためです。 didSelectCheckmarkを設定する代わりに、cellForRowAtIndexPathに設定してみます。また、あなたはあなたの問題

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

を解決するために、このようなmodelクラスを作成する必要があります今すぐ下記のよう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 
} 

は、今これはあなたを助けるこの

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

希望のようなあなたのdidSelectを変更します。

0

uはビューの外のセルをスクロールして戻ってスクロールしたときに、それはあなたが作成されて何をすべきか、もう一度cellForRowを呼び出し、デフォルトに戻すすべてを設定しますので、それはです適切なdataSource、セルがチェックされるたびに、チェックされたことを示すBoolでdataSourceを更新してから、cellForRowまたはcellWillDisplay

+0

私のcellForRowAtIndexPathには、:accesoryTypeとして.Noneがあります。私はこれをコメントしており、それは動作するようです。 @ Dev.RKは、私のcellForRowAtIndexPathメソッドを投稿するように助けてくれました – Wayne

+0

その練習はUIを維持するためだけであり、あなたはUI自体ではなくデータソースを更新する必要があります.Niravの答えはそれを行う上での基本を示します – Tj3n

関連する問題