2015-09-30 40 views
6

Tôi có 2 phần trong UITableView của mình.
Tôi muốn phần đầu tiên cho phép chọn nhiều ô và phần thứ hai chỉ cho phép chọn một lần.
Tôi đã thử một số mã nhưng không hoạt động tốt.
Mã nhanh nếu có thể. Cảm ơn bạn.UITableView - Nhiều lựa chọn VÀ lựa chọn duy nhất

enter image description here

+0

Bạn có muốn phần thứ hai để bỏ chọn hàng đầu tiên khi hàng thứ hai được chọn? Ngoài ra, việc đăng mã hiện tại của bạn sẽ giúp ích. – Caleb

Trả lời

4

Có lẽ bạn có thể thực hiện quan điểm của bảng phương pháp đại biểu:

tableView(_:shouldHighlightRowAtIndexPath:)

tableView(_:didSelectRowAtIndexPath:)

... và xác định (từ indexPath.rowindexPath.section) nếu phần có liên quan hỗ trợ s ingle/multiple selection (điều này sẽ phụ thuộc vào tùy chọn logic của mô hình dữ liệu của bạn: "Section 0 hỗ trợ nhiều lựa chọn nhưng phần 1 không") và nếu nó chỉ hỗ trợ lựa chọn duy nhất, hãy kiểm tra xem đã chọn một hàng chưa (bằng cách truy cập tableView.indexPathsForSelectedRows).

Nếu có một hàng đã chọn rồi, bạn có thể:

  1. Return false từ tableView(_:shouldHighlightRowAtIndexPath:), và
  2. Không làm gì (chỉ return) từ tableView(_:didSelectRowAtIndexPath:) (Tôi không chắc chắn nếu phương pháp này là thực sự gọi khi bạn trả lại false từ shouldHighlight..., vì vậy có lẽ hãy kiểm tra lại).
0

Nếu bạn muốn hàng đã chọn trong phần 2 là hàng mới được chọn, điều này có thể phù hợp với bạn. Khác, đi với câu trả lời của @ NicolasMiari.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if indexPath.section == 1 { 
     for i in 0..tableView.numberOfRowsInSection(indexPath.section) - 1 { 
      let cell: UITableViewCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: i, inSection: indexPath.section))! 
      if (i == indexPath.row) { 
       cell.accessoryType = .Checkmark 
       cell.selected = false 
      } 
      else { 
       cell.accessoryType = .None 
      } 
     } 
    } 
    else { 
     //Do whatever for the first section 
    } 
} 

Không thanh lịch, nhưng hy vọng nó sẽ cung cấp cho bạn ý tưởng.

2

Bạn chỉ cần thử điều này. Giải pháp này làm việc cho tôi một cách hoàn hảo. Cung cấp cho nó một thử có thể làm việc cho người khác ...

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

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

được sửa đổi: Đối với Swift-4

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if indexPath.section == 0 { 
     if let cell = tableView.cellForRow(at: indexPath) { 
      cell.accessoryType = .checkmark 
     } 
    } 
    else { 
     if let cell = tableView.cellForRow(at: indexPath) { 
      cell.accessoryType = .checkmark 
     } 
    } 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    if indexPath.section == 1 { 
     if let cell = tableView.cellForRow(at: indexPath as IndexPath) { 
      cell.accessoryType = .none 
     } 
    } 
} 
Các vấn đề liên quan