2017-03-24 4 views
0

내 개발 환경은 swift3, xcode8입니다. 나는 Apple의 메시지 앱과 같은 목록 응용 프로그램을 만들고 있습니다. 테이블보기에서 목록을 선택하면 세부 페이지 (seg를 통해)로 이동합니다 이제 여러 개의 삭제 기능을 구현하고 싶습니다.swift - 어떻게 segue없이 tableview에서 다중 행을 선택할 수 있습니다

하지만 문제가 있습니다. 모드를 편집 할 때 선택 창이 표시됩니다. 하지만 해당 선택 창을 선택하면 세부 정보 페이지로 이동하십시오.

세그 을 통해 세부 정보 페이지로 이동하기 전에 여러 선택을해야한다고 생각합니다. 어떻게해야합니까?

답변

1

다음 코드와 일치하는지 확인하십시오.

class TableviewController:UITableViewController{ 
override func viewDidLoad() { 
    super.viewDidLoad() 
    var isMultipleSelectionActive = false 
    var selectedItems: [String: Bool] = [:] 
    tableView.allowsMultipleSelectionDuringEditing = true 
    tableView.setEditing(true, animated: false) 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 
    cell.textLabel?.text = "\(indexPath.row)" 
    return cell 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let selectedItem = items.objectAtIndex(indexPath.row) 
    //add to selectedItems 
    selectedItems[selectedItem] = true 
} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    let selectedItem = items.objectAtIndex(indexPath.row) 
    // remove from selectedItems 
    selectedItems[selectedItem] = nil 
} 

func getStatusOfSelectedItems() { 
    for item in selectedItems { 
     println(item) 
    } 
} 

//You should override shouldPerformSegueWithIdentifier and return false if isMultipleSelectionActive is true 

override func shouldPerformSegue(withIdentifier identifier: String?, sender: Any?) -> Bool { 
    if let identifierName = identifier { 
     if identifierName == "NameOfYourSegueIdentifier" { 
      if isMultipleSelectionActive { 
        return false 
      } 
     } 
    } 
    return true 
} 
} 
+0

감사를 선택하는 데 사용이 코드. 그러나이 코드는 내가 필요한 소스를 찾지 못했습니다. segue가 작동하지 않는 경우에만 여러 선택이 잘 작동합니다. 편집 모드에서 다중 선택에서 segue로 이동하지 못하게 할 방법이 있습니까? –

+0

당신이 어떻게 segue의 코드를 보여줄 수 있습니까 ?? –

+0

내 코드를 어떻게 보여줄 수 있습니까? 내가 초보자이기 때문에 잘 모른다. 컨트롤 + 마우스 왼쪽 버튼으로 스토리 보드에서 셀을 드래그하여 세부 정보 페이지로 드래그 한 다음 쇼를 선택하여 세그먼트를 만들었다. –

0

여러 행에게 답장을 보내

class TableViewController: UITableViewController 
{ 
    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0) 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) 

     // Configure the cell... 
     cell.textLabel!.text = "row: \(indexPath.row)" 

     if cell.selected 
     { 
      cell.selected = false 
      if cell.accessoryType == UITableViewCellAccessoryType.None 
      { 
       cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
      } 
      else 
      { 
       cell.accessoryType = UITableViewCellAccessoryType.None 
      } 
     } 

     return cell 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     let cell = tableView.cellForRowAtIndexPath(indexPath) 

     if cell!.selected 
     { 
      cell!.selected = false 
      if cell!.accessoryType == UITableViewCellAccessoryType.None 
      { 
       cell!.accessoryType = UITableViewCellAccessoryType.Checkmark 
      } 
      else 
      { 
       cell!.accessoryType = UITableViewCellAccessoryType.None 
      } 
     } 
    } 
+0

답장을 보내 주셔서 감사합니다. 하지만 문제는 편집 모드에서 체크인하면 체크되지 않았지만 셀의 세그 조작으로 세부 정보 페이지로 이동한다는 것입니다. 편집 작업 상태가 아닌 편집 상태에서 확인하고 싶습니다. 섹터없이 컴파일하는 경우 여러 선택이 잘 작동합니다. –