2014-12-19 1 views
1

나는 많은 시간을 낭비 했으므로 좌절감을 털어 놓는다. allowMultipleSelection이 내 TableView에서 작동하지 않는 이유를 모르겠습니다. 작동시키기 위해 모든 일을해야하지만, 여전히 결과가 없습니다. 제 코드를 살펴보고 친절하게 문제를 알려주세요.Allow MultipleSelection이 Swift에서 작동하지 않습니까?

override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     self.participantsTableView.allowsMultipleSelection = true 
} 

if tableView == participantsTableView 
     { 
      var cell = tableView.cellForRowAtIndexPath(indexPath)! 

      if cell.accessoryType == UITableViewCellAccessoryType.Checkmark 
      { 
       cell.accessoryType = UITableViewCellAccessoryType.None 
      } 

      else 
      { 
       cell.accessoryType == UITableViewCellAccessoryType.Checkmark 
      } 

      self.participantsTableView.reloadData() 
     } 
+0

은 어디 코드'== participantsTableView ~있는 tableView 경우'을 배치해야합니까? 함수'tableView : didSelectRowAtIndexPath :'? 그렇다면 테이블을 다시로드 할 필요가 없습니다. – bluedome

+0

"didSelectRowAtIndexPath"에서 예 및 나는 tableView를 다시로드하지 않고 동일하게 시도했습니다! – Developer

+0

pls는 테이블의 데이터 소스 메소드 인'tableView : cellForRowAtIndexPath :'에 코드를 게시합니다. – bluedome

답변

2

당신은 대리자 메서드 tableView:didDeselectRowAtIndexPath:tableView:cellForRowAtIndexPath:에서 업데이트 세포의 accessoryType를 구현해야합니다. 이 같은

:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.accessoryType = .Checkmark 
    } 
} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.accessoryType = .None 
    } 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath) as UITableViewCell 
    cell.textLabel?.text = String(indexPath.row) 

    if let selectedPaths = tableView.indexPathsForSelectedRows() as? [NSIndexPath] { 
     let selected = selectedPaths.filter(){ $0 == indexPath } 
     if selected.count > 0 { 
      cell.accessoryType = .Checkmark 
     } else { 
      cell.accessoryType = .None 
     } 
    } 
    return cell 
} 
관련 문제