2012-09-14 18 views
10

मुझे पता है कि UITableViewCell का उप-वर्ग willTransitionToState लागू कर सकता है और संक्रमण के समय कस्टम कोड निष्पादित कर सकता है। लेकिन क्या सेल की वर्तमान स्थिति को खोजने का कोई तरीका है?सेल की वर्तमान स्थिति निर्धारित करना

यदि नहीं, तो मुझे UITableViewCell उपclass करना चाहिए और currentState संपत्ति को परिभाषित करना चाहिए, जिसे मैं हमेशा अपने willTransitionToState में अपडेट करता हूं? तब मुझे हमेशा किसी विशेष सेल की स्थिति जानने का एक तरीका होगा।

अजीब लगता है कि मैं एक सेल से नहीं पूछ सकता कि इसकी वर्तमान स्थिति क्या है (0, 1, 2, या 3)।

उत्तर

15

वर्तमान राज्य हैं UITableViewCellStateDefaultMask (0), UITableViewCellStateShowingEditControlMask (1), UITableViewCellStateShowingDeleteConfirmationMask (2), और UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask (3)।

ये राज्य गुणों के मूल्यों के अनुरूप हैं editing और showingDeleteConfirmation। यह इस प्रकार है के रूप में परीक्षण किया जा सकता:

if (!cell.editing && !cell.showingDeleteConfirmation) { 
    // 0 - UITableViewCellStateDefaultMask 
} else if (cell.editing && !cell.showingDeleteConfirmation) { 
    // 1 - UITableViewCellStateShowingEditControlMask 
} else if (!cell.editing && cell.showingDeleteConfirmation) { 
    // 2 - UITableViewCellStateShowingDeleteConfirmationMask 
} else if (cell.editing && cell.showingDeleteConfirmation) { 
    // 3 - UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask 
} 
+1

कई धन्यवाद। मेरा अंतिम लक्ष्य उन कोशिकाओं की पहचान करना है जो हटाए गए पुष्टिकरण को दिखा रहे हैं क्योंकि सेल स्वाइप किया गया था (और इसलिए नहीं क्योंकि संपादन नियंत्रण का उपयोग किया गया था)। इन विकल्पों को देखते हुए, ऐसा लगता है कि यह पर्याप्त रूप से पर्याप्त नहीं होगा, और दोनों मामलों में राज्य (3) (सेल और टेबलव्यू दोनों परिदृश्यों में 'संपादन' दोनों हैं)। ड्रॉइंग बोर्ड पर वापस। –

8

iOS 6 के लिए, यहाँ मेरी समाधान है:

संक्रमण राज्यों में से किसी के लिए

काम करता है और साथ ही इशारा हटाने के लिए स्वाइप संभालती है। इस कोड को UITableviewCell के अपने उप-वर्ग में रखें।

- (void)willTransitionToState:(UITableViewCellStateMask)state { 

    [super willTransitionToState:state]; 

    if (state == UITableViewCellStateDefaultMask) { 

     NSLog(@"Default"); 
     // When the cell returns to normal (not editing) 
     // Do something... 

    } else if ((state & UITableViewCellStateShowingEditControlMask) && (state & UITableViewCellStateShowingDeleteConfirmationMask)) { 

     NSLog(@"Edit Control + Delete Button"); 
     // When the cell goes from Showing-the-Edit-Control (-) to Showing-the-Edit-Control (-) AND the Delete Button [Delete] 
     // !!! It's important to have this BEFORE just showing the Edit Control because the edit control applies to both cases.!!! 
     // Do something... 

    } else if (state & UITableViewCellStateShowingEditControlMask) { 

     NSLog(@"Edit Control Only"); 
     // When the cell goes into edit mode and Shows-the-Edit-Control (-) 
     // Do something... 

    } else if (state == UITableViewCellStateShowingDeleteConfirmationMask) { 

     NSLog(@"Swipe to Delete [Delete] button only"); 
     // When the user swipes a row to delete without using the edit button. 
     // Do something... 
    } 
} 
+0

आईओएस 8.1 में एसडीके (राज्य == यूआईटीबल व्यूसेलस्टेटशोइंगडिलेट कॉन्फर्मेशन मास्क) को बदलना चाहिए (राज्य और यूआईटीबल व्यूसेलस्टेटशोइंगडिलेट कॉन्फर्मेशन मास्क) क्योंकि उच्च बिट पोजिशन शून्य के कारण नहीं हैं। – Neil

0

नील की टिप्पणी के साथ jhilgert00 की तेज संस्करण लागू:

override func willTransitionToState(state: UITableViewCellStateMask) { 

    super.willTransitionToState(state) 

    if (state == UITableViewCellStateMask.DefaultMask) { 
     println("default") 
    } else if (state & UITableViewCellStateMask.ShowingEditControlMask != nil) 
     && (state & UITableViewCellStateMask.ShowingDeleteConfirmationMask != nil) { 
      println("Edit Control + Delete Button") 
    } else if state & UITableViewCellStateMask.ShowingEditControlMask != nil { 
     println("Edit Control Only") 
    } else if state & UITableViewCellStateMask.ShowingDeleteConfirmationMask != nil { 
     println("Swipe to Delete [Delete] button only") 
    } 
} 
0

तेज 3 से शुरू राज्य का मान एक OptionSet, तो आप इस तरह उपयोग कर सकते हैं:

override func willTransitionToState(state: UITableViewCellStateMask) { 
    super.willTransitionToState(state) 
    if state.contains(.DefaultMask) { 
     print("DefaultMask") 
    } 
    if state.contains(.ShowingEditControlMask) { 
     print("ShowingEditControlMask") 
    } 
} 
संबंधित मुद्दे