2015-09-30 16 views
6

मेरे पास मेरे UITableView में 2 अनुभाग हैं।
मैं पहले अनुभाग को एकाधिक सेल चयन और दूसरे खंड को केवल एकल चयन की अनुमति देने की अनुमति देता हूं।
मैंने कुछ कोड की कोशिश की लेकिन बहुत अच्छी तरह से काम नहीं किया।
यदि संभव हो तो स्विफ्ट में कोड। धन्यवाद।UITableView - एकाधिक चयन और एकल चयन

enter image description here

+0

के लिए आप दूसरे खंड प्रथम पंक्ति का चयन रद्द करने के लिए जब दूसरी पंक्ति चयन किया जाता है करना चाहते हैं? साथ ही, अपना मौजूदा कोड पोस्ट करने से मदद मिलती है। – Caleb

उत्तर

4

शायद आप को लागू कर सकता है तालिका दृश्य के प्रतिनिधि विधि:

tableView(_:shouldHighlightRowAtIndexPath:)

और

tableView(_:didSelectRowAtIndexPath:)

... और तय करें कि (indexPath.row और indexPath.section से) प्रासंगिक खंड एस का समर्थन करता है इंजेल/एकाधिक चयन (यह आपके डेटा मॉडल के कस्टम तर्क पर निर्भर करेगा -eg: "सेक्शन 0 एकाधिक चयन का समर्थन करता है लेकिन धारा 1") नहीं है, और यदि यह केवल एकल चयन का समर्थन करता है, तो जांचें कि पहले से ही एक पंक्ति चयनित है (एक्सेस करके tableView.indexPathsForSelectedRows)।

अगर वहाँ एक चयनित पंक्ति पहले से ही है, तो आप: tableView(_:shouldHighlightRowAtIndexPath:) से

  1. वापसी false, और
  2. tableView(_:didSelectRowAtIndexPath:) से कुछ भी नहीं है (बस return) करते हैं (मुझे यकीन है कि नहीं कर रहा हूँ इस विधि वास्तव में कहा जाता है, तो जब आप falseshouldHighlight... से वापस लौटते हैं, तो शायद इसे जांचें)।
0

यदि आप अनुभाग 2 में चयनित पंक्ति को नई चयनित पंक्ति के रूप में देखना चाहते हैं, तो यह आपके लिए काम कर सकता है। अन्यथा, @ निकोलसमीरी के जवाब के साथ जाओ।

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 
    } 
} 

बहुत सुरुचिपूर्ण नहीं है, लेकिन उम्मीद है कि यह आपको एक विचार देगा।

2

आप बस इसे आज़मा सकते हैं। यह समाधान मेरे लिए पूरी तरह से काम करता है। यह एक कोशिश शायद दूसरों के लिए काम किया दें ...

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 
     } 
    } 
} 

संपादित: स्विफ्ट-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 
     } 
    } 
} 
संबंधित मुद्दे