2016-06-01 16 views
0

ここで私はtableview multiselectの問題を抱えています。私は特定の行数(リストから5行)のテーブルビューの選択を制限したい。選択された行は.blueとして選択スタイルにする必要があります、6行目を選択しようとすると、行選択スタイルは.none.butでなければなりませんsomewayとうまく動作していないと試してみました。ios swiftのuitableview multiselctの選択された行数に基づいて選択スタイルを制限する方法

これはcellForRowAtIndexPath方法、

if SelectedArray.count <= 5 
{ 
    cell.selectionStyle = UITableViewCellSelectionStyle.Blue 
} 
else { 
     cell.selectionStyle = UITableViewCellSelectionStyle.None 
} 

またdidSelectRowAtIndexPathこの上記の宣言を定義、willDisplayCell

​​

私のコードであり、また場合didselectrow

も、この私のテーブルをリロードdidSelectRowAtIndexPathメソッドで試したところ、

self.tableView(tableView, willDisplayCell: cell, forRowAtIndexPath: indexPath) 

しかし、それの使用はない、plsは私に事前に

感謝を助けます。

+0

をご覧ください。あなたは、<= 5の代わりに、<を試してみてください。 – sanman

答えて

1

詳細については、この1

func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 

    if let sr = tableView.indexPathsForSelectedRows { 
     if sr.count == limit { 
      let alertController = UIAlertController(title: "Oops", message: 
       "You are limited to \(limit) selections", preferredStyle: .Alert) 
      alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: {action in 
      })) 
      self.presentViewController(alertController, animated: true, completion: nil) 

      return nil 
     } 
    } 

    return indexPath 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    print("selected \(intervalNames[indexPath.row])") 

    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     if cell.selected { 
      cell.accessoryType = .Checkmark 
     } 
    } 

    if let sr = tableView.indexPathsForSelectedRows { 
     print("didDeselectRowAtIndexPath selected rows:\(sr)") 
    } 
} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 

    print("deselected \(intervalNames[indexPath.row])") 

    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.accessoryType = .None 
    } 

    if let sr = tableView.indexPathsForSelectedRows { 
     print("didDeselectRowAtIndexPath selected rows:\(sr)") 
    } 
    } 

} 

を試してみてください、このリンクhttps://github.com/genedelisa/LimitTableExample

+0

ありがとうございました –

+0

It's my plasure –

関連する問題