2017-07-18 4 views
-1

UITableviewの2つのセルを一度に選択できるようにすることはできますか?現在、私は単一選択またはUITableViewの複数の選択を設定することができます。UITableviewの2つのセルだけを一度に選択できるようにする方法

これについてSwift3にアイデアやコードを投稿できますか?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier") as! UITableViewCell 
    let currentItem = data[indexPath.row] 
    if currentItem.selected { 
     cell.imageView!.image = UIImage(named:"check")! 
     cell.textLabel!.font = UIFont(name:"OpenSans-Bold", size:15) 
    } else { 
     cell.imageView!.image = nil 
     cell.textLabel!.font = UIFont(name:"OpenSans-Regular", size:15) 
    } 

    return cell 
    } 

答えて

1

セルが選択されると、コールバックはdidSelectRowAtIndexになります。選択したセルを追跡し、それに応じてセルを選択することができます。配列を使用して、選択したすべてのセルを追跡する

var selectedIndexes = [Int]() 


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     if (selectedIndexes.contains(indexPath.row)) { 
      selectedIndexes.remove(at: selectedIndexes.index(of: indexPath.row)!) 
     } else { 
      if selectedIndexes.count == 2 { 
       selectedIndexes[0] = indexPath.row 
      } else { 
       selectedIndexes.append(indexPath.row) 
      } 

     } 
     tableView.reloadData() 
} 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier") as! UITableViewCell 
    let currentItem = data[indexPath.row] 
    if selectedIndexes.contains(indexPath.row) { 
     cell.imageView!.image = UIImage(named:"check")! 
     cell.textLabel!.font = UIFont(name:"OpenSans-Bold", size:15) 
    } else { 
     cell.imageView!.image = nil 
     cell.textLabel!.font = UIFont(name:"OpenSans-Regular", size:15) 
    } 

    return cell 
    } 
+0

この操作方法は? –

+0

詳細を入力して回答を更新します –

+0

ありがとう...本当に役に立ちます –

関連する問題