2011-12-30 8 views
5

मैंने कोर डेटा के साथ आबादी वाली एक तालिका लागू की है और अब मैं पक्ष वर्णमाला (स्वरूप जैसे संपर्कों में) प्रदर्शित करके अनुभागों के साथ अनुक्रमणित करने का प्रयास कर रहा हूं।कोर डेटा में सेक्शन इंडेक्स के लिए पूरे वर्णमाला

नीचे दिए गए कोड में, यदि मैं टिप्पणी की गई रेखा का उपयोग करता हूं, तो मेरे पास मौजूदा अनुभागों के लिए केवल अक्षर हैं। लेकिन मैं पूरी वर्णमाला चाहते हैं, और इसलिए मैं वापस आ सरणी बदल दिया है:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{  
    //return [fetchedResultsController sectionIndexTitles];  

    indexArray = [NSArray arrayWithObjects: @"{search}", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J",@"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"#", nil]; 
    return indexArray; 
} 

सभी पत्र पक्ष सूचकांक पर प्रदर्शित होते हैं। लेकिन अब मैं विधि है कि चयनित खंड के सूचकांक रिटर्न लागू करने के लिए है, और यहाँ मैं कुछ समस्याओं का है: ऊपर

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    //return [fetchedResultsController sectionForSectionIndexTitle:title atIndex:index]; 

    NSString *correspondingLetter = [indexArray objectAtIndex:index]; 
    NSUInteger correspondingIndex = [[fetchedResultsController sections] indexOfObject:correspondingLetter]; 

    NSLog(@"------index:%i\ncorrespondingLetter: %@\ncorrespondingIndex: %i\n", index,correspondingLetter, correspondingIndex); 

    return correspondingIndex; 
} 

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

अग्रिम धन्यवाद, yassa

उत्तर

5

आप

@property (nonatomic, readonly) NSArray *sectionIndexTitles 
fetchedResultController की

में खोज करनी चाहिए।
लेकिन, अगर यह एक सूचकांक जो मौजूद नहीं है आप NSNotFound प्राप्त होगा और पिछले पत्र सूचकांक, या पिछले या पिछले वापस जाने के लिए कुछ तर्क करने की आवश्यकता होगी, आदि

+1

अविश्वसनीय, यह मेरे सामने था और मैंने इसे नहीं देखा !! :) जैसा कि आपने सुझाव दिया था, यह निम्नलिखित करने के लिए पर्याप्त था: 'इसी इंडेक्स = [[fetchedResultsController sectionIndexTitles] indexOfObject: relatedLetter];'। बहुत बहुत धन्यवाद !!! – yassassin

2

अतिरिक्त भाषाओं को समर्थन करने के लिए है मैं हार्ड कोडित वर्णों का उपयोग नहीं करेंगे। स्वीकार्य उत्तर से प्रेरित मेरा समाधान:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; 
} 


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    NSString *correspondingLetter = [[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles] objectAtIndex:index]; 
    return [[self.fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter]; 
}