2009-09-24 24 views
32

में सम्मिलित पंक्तियों का उपयोग करके मैं अपने UITableView को संपर्क संपादक में तालिका की तरह व्यवहार करना चाहता हूं, यानी उपयोगकर्ता को संपादित करना चाहिए और प्रत्येक अनुभाग के नीचे "नई श्रेणी जोड़ें" पंक्ति दिखाई देनी चाहिए।UITableView

मैं ऐसा करने के लिए नीचे दिए गए कोड का उपयोग कर रहा हूं, लेकिन समस्या यह है कि संपर्कों में कोई चिकनी संक्रमण नहीं है। इसके बजाय, नई पंक्ति अचानक प्रकट होती है। मैं एनीमेशन कैसे प्राप्त कर सकता हूं?

इसके अलावा, मैं "नई श्रेणी जोड़ें" पंक्ति पर क्लिक का जवाब कैसे दूं? पंक्ति मेरे वर्तमान कार्यान्वयन में क्लिक करने योग्य नहीं है।

क्या उपयोगकर्ता डेटा संपादन शुरू करते समय डेटा को फिर से लोड करने की आवश्यकता है? मैं ऐसा इसलिए कर रहा हूं क्योंकि अन्यथा सम्मिलन पंक्तियां कभी नहीं खींची जाती हैं।

धन्यवाद।

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; 
    [tableView reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section { 
    // ... 
    if(self.tableView.editing) 
     return 1 + rowCount; 
} 

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // ..... 
    NSArray* items = ...; 
    if(indexPath.row >= [items count]) { 
     cell.textLabel.text = @"add new category"; 
    } 
    // ... 

    return cell; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSArray* items = ...; 

    if(indexPath.row == [items count]) 
     return UITableViewCellEditingStyleInsert; 

    return UITableViewCellEditingStyleDelete; 
} 
+2

यह बहुत उपयोगी (नीचे उत्तर के साथ-साथ) था:

// Assuming swipeMode is a BOOL property in the class extension. - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { // Invoked only when swiping, not when pressing the Edit button. self.swipeMode = YES; } - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { self.swipeMode = NO; } 

आपका कोड एक छोटा सा परिवर्तन की आवश्यकता होगी। बस एक मामूली असंगतता - 'तालिका दृश्य में पंक्ति गणना की तुलना: सेलफॉररोएट इंडेक्सपैथ:' '> =' का उपयोग करता है, जबकि 'तालिका दृश्य में: संपादन स्टाइलफॉररोएट इंडेक्सपैथ:' '==' का उपयोग करता है। एक बड़ा सौदा नहीं है, लेकिन उनके बीच सुसंगत होना चाहिए। '> =' सम्मिलित पंक्ति के किसी आकस्मिक डबल-जोड़ को कवर करेगा, जबकि '==' किसी भी कोड त्रुटियों के लिए अपवाद फेंकने में मदद करेगा जो उस स्थिति का कारण बन सकता है। –

उत्तर

36

मुझे एक चीज़ याद आ रही थी। setEditing में :, बजाय reloadData बुलाने की मैं करना चाहिए था:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController 

    NSMutableArray* paths = [[NSMutableArray alloc] init]; 

    // fill paths of insertion rows here 

    if(editing) 
     [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
    else 
     [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 

    [paths release]; 
} 
+3

धन्यवाद बिल, यह मेरे लिए बहुत उपयोगी था। मैंने इसी तरह के कोड में कई घंटे बिताए लेकिन इसे काफी सही नहीं मिला। इससे अनावश्यक रूप से मदद मिली। – hkatz

+0

खुशी हुई यह मदद की! – Bill

+0

प्रतिबद्ध करने के बारे में क्या संपादन शैली: forRowAtIndexPath? ऐप्पल प्रलेखन का कहना है कि सेट एडिटिंग को तब कहा जाता है जब टेबल व्यू पहले संपादन मोड में जाता है ... आप कैसे जानते होंगे कि उस बिंदु पर कौन सी पंक्तियां डालने/हटाना है? – shim

5

पंक्ति पर क्लिक करता है का जवाब देते didSelectRowAtIndexPath विधि से किया जा सकता है, indexPath.row == [items count] के लिए। एनीमेशन के लिए, मैं insertRowsAtIndexPaths:withRowAnimation: विधि पर, here पर एक नज़र डालने का सुझाव देता हूं। here का उपयोग करने के तरीके पर एक पोस्ट है।

0

हाइलाइट किए गए समाधान का एक अवांछित दुष्प्रभाव यह है कि "जोड़ें" जब उपयोगकर्ता सिर्फ एक ही पंक्ति स्वाइप पंक्ति भी डाला जाता है है (स्वाइप कि प्रदान की सक्षम किया गया है)। निम्नलिखित कोड इस दुविधा को हल करती है:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController 

    if (!self.swipeMode) { 

     NSMutableArray* paths = [[NSMutableArray alloc] init]; 

     // fill paths of insertion rows here 

     if(editing) 
      [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
     else 
      [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
     [paths release]; 
    } 
}