9

क्या का उपयोग कर का उपयोग कर NSMutableDictionary में किसी मान को संग्रहीत करने और पुनर्प्राप्त करने का कोई विशेष कारण है?NSIndexPath का उपयोग एनएसएमयूटेबल डिक्शनरी में कुंजी के रूप में करते हैं?

मैं मूल रूप से आदेश में एक UITableView के लिए एक UITableViewCell की NSMutableDictionary ऊंचाइयों (self.cellHeights) स्टोर करने के लिए में ऐसा करने का प्रयास किया। हर बार जब आप एक UITableViewCell उपयोग किया, उस कक्ष या तो विस्तार या मूल्य उस विशेष indexPath के लिए NSMutableDictionary में संग्रहीत के आधार पर दो अलग-अलग ऊंचाइयों के बीच अनुबंध होगा:

- (CGFloat)tableView:(UITableView *)tableView 
      heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSNumber *heightNSNumber = [self.cellHeights objectForKey:indexPath]; 
    if (!heightNSNumber) 
    { 
     heightNSNumber = [NSNumber numberWithFloat:100.0]; 
     [self.cellHeights setObject:heightNSNumber forKey:indexPath]; 
    } 
    return [heightNSNumber floatValue]; 
} 

- (void)tableView:(UITableView *)tableView 
     didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    NSNumber *heightNSNumber = [self.cellHeights objectForKey:indexPath]; 
    if (!heightNSNumber) 
    { 
     heightNSNumber = [NSNumber numberWithFloat:100.0]; 
     [self.cellHeights setObject:heightNSNumber forKey:indexPath]; 
    } 

    if ([heightNSNumber floatValue] == 100.0) 
    { 
     [self.cellHeights setObject:[NSNumber numberWithFloat:50.0] 
          forKey:indexPath]; 
    } else { 
     [self.cellHeights setObject:[NSNumber numberWithFloat:100.0] 
          forKey:indexPath]; 
    } 
    [tableView beginUpdates]; 
    [tableView endUpdates]; 
} 

मेरे लिए अज्ञात कारणों से, tableView:didSelectRowAtIndexPath: भीतर सेल ऊंचाई हो रही [self.cellHeights objectForKey:indexPath] के माध्यम से बस ठीक काम करता है। हालांकि, tableView:heightForRowAtIndexPath: के माध्यम से tableView:heightForRowAtIndexPath: के माध्यम से सेल ऊंचाई प्राप्त करने का प्रयास हमेशा शून्य हो जाता है क्योंकि ऐसा लगता है कि इंडेक्सपैथ ऊंचाई को स्टोर करने के लिए उपयोग किया जाता है, जो सेल की ऊंचाई लाने के लिए उपयोग किए जाने वाले इंडेक्सपैथ से मेल नहीं खाता है, भले ही उनके पास indexPath.section के लिए समान मान हों और indexPath.row। इस वजह से, "समान" सूचकांक पथ के लिए एक नई वस्तु self.cellHeights में जोड़ा गया है (जैसा कि self.cellHeights.count उसके बाद बढ़ता है)।

यह करता नहीं हो सकता है जब आप NSMutableDictionary में सेल ऊंचाइयों कुंजी के रूप में पंक्ति ([NSNumber numberWithInteger:indexPath.row]) का उपयोग कर स्टोर ... इसलिए मैं अब के लिए क्या कर रहा है कि, लेकिन मैं क्यों समझने के लिए indexPath करना चाहते हैं कुंजी के रूप में काम नहीं कर रहा है।

उत्तर

1

वहाँ कई चीजें हैं जो एक वस्तु NSDictionary के लिए एक महत्वपूर्ण है, अर्थात् isEqual:, hash और Copyable प्रोटोकॉल के रूप में मज़बूती से काम करने के लिए लागू किया जाना चाहिए रहे हैं।

मुझे पूरा यकीन नहीं है कि NSIndexPath कभी भी शब्दकोशों के लिए कुंजी के रूप में काम करने का इरादा रखता था (क्योंकि यह सरणी के लिए एक सूचकांक बन गया था)।

मेरा अनुमान है कि hash कक्षा के विभिन्न उदाहरणों के लिए सही ढंग से लागू नहीं किया गया है। यह भी ध्यान रखें कि कुछ तालिका प्रतिनिधि विधियों को NSIndexPath और कुछ NSMutableIndexPath के साथ बुलाया जाता है। यह शायद अंतर बना रहा है।

12

हालांकि मुझे चर्चा में देर हो चुकी है, यहां एक त्वरित और सरल समाधान है जो आपको एनएसआईएंडएक्सपैथ उदाहरणों को शब्दकोश कुंजी के रूप में उपयोग करने की अनुमति देगा।

बस निम्न पंक्ति जोड़कर indexPath पुन:

indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]; 

Voilà। tableView:heightForRowAtIndexPath: आंतरिक रूप से NSMutableIndexPath उदाहरणों का उपयोग करता है (जैसा कि आप ब्रेकपॉइंट के साथ देखेंगे)। हैश कुंजी की गणना करते समय किसी भी तरह वे उदाहरण NSIndexPath के साथ असंगत प्रतीत होते हैं।

इसे वापस NSIndexPath पर परिवर्तित करके, सब कुछ काम करता है।

+0

पवित्र Moly: वैकल्पिक हल हमेशा कुछ भी है कि इस तरह के चाबियाँ शब्दकोश को देख के रूप में isEqual या hash पर निर्भर करता है, के लिए एक महत्वपूर्ण NSIndexPath उत्पन्न करने के लिए है। यह मुझे छोड़ रहा था। घंटों तक मैं यह समझने की कोशिश कर रहा हूं कि मेरा शब्दकोश एक इंडेक्स के लिए शून्य क्यों लौट रहा था। मैं केएनयूई अस्तित्व में था। बिल्कुल गड़बड़ाना पोस्ट करने का शुक्रिया। – Andy

3

@ जीन का जवाब स्वीकार्य लगता है, लेकिन यह प्रश्न answered in more detail here रहा है। संक्षेप में, UITableView कभी-कभी NSIndexPath के बजाय NSMutableIndexPath के उदाहरणों का उपयोग करता है और इन दो वर्गों के उदाहरण कभी बराबर नहीं होते हैं क्योंकि [NSMutableIndexPath class] != [NSIndexPath class]

- (NSIndexPath *)keyForIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([indexPath class] == [NSIndexPath class]) { 
     return indexPath; 
    } 
    return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]; 
} 
संबंधित मुद्दे