2013-11-18 3 views
0

я не имею Tableview с разрезами в нем от A до Z (без секций не фиксированы я рассчитывается динамически)Дисплей сотового мудрая врезается приложение IOS

я хочу показать, как это: : Мои ценности массива : msg_array = [ "AajKaCatch", "AajKaItem", "Anari", "Big C черни", "Большой Flix", "Bigrock", "caksonflowers, ...."]

, когда я пытаюсь отобразите это как cellForRowAtIndexPath, это показывает NSInvalidArgumentException

cell.textLabel.text=[[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Merchant_Name"];

пожалуйста, помогите и спасибо заранее.

+0

msg_array содержит только строковые объекты. Он должен содержать объекты NSArray, которые будут содержать объекты типа NSDictionary. –

ответ

1

Ваш массив как:

array{object,object,object,object,object}; 

В такой ситуации, вы можете» t использовать:

[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] 

Потому что для реализации такого, [msg_array objectAtIndex:indexPath.section] должен возвращать массив.

Так реализации этого, вам нужно попробовать так:

array{array{objects starts with 'A'},array{objects starts with 'B'},array{objects starts with 'C'}}; 
+0

' - (void) createAlphabetArray { NSMutableArray * tempFirstLetterArray = [[NSMutableArray alloc] init]; for (int i = 0; i <[msg_array count]; i ++) { NSMutableArray * temp = [[NSMutableArray alloc] init]; NSString * letterString = [[[msg_array objectAtIndex: i] objectForKey: @ "Merchant_Name"] substringToIndex: 1]; if (! [TempFirstLetterArray containsObject: [letterString uppercaseString]]) { [tempFirstLetterArray addObject: letterString]; [temp addObject: [[msg_array objectAtIndex: i] objectForKey: @ "Merchant_Name"]]; } } } ' – Krunal

+0

Вы правы, но как добавить элементы в массив? выше мой код, который я написал, но добавляет только первый элемент символа – Krunal

+0

@Krunal: в вашем коде блок if будет true, когда он найдет новый символ, во всех остальных случаях он будет ложным. поэтому он не будет добавлять другие объекты –

1

Когда вы делаете это:

[[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Merchant_Name"]; 

Вы обращаетесь к элементу msg_array, как если бы это был NSArray, который содержит NSDictionary.

Но, внутри msg_array у вас только есть NSStrings.

Структура вы triying доступа является:

NSArray -> NSArray -> NSDictionary 

И у вас есть

NSArray -> NSString 
+0

Вы правы, но как добавить элементы в массив? ниже мой код, который я написал, но он добавляет только первый элемент символа. – Krunal

+0

'- (void) createAlphabetArray {NSMutableArray * tempFirstLetterArray = [[NSMutableArray alloc] init]; for (int i = 0; i <[msg_array count]; i ++) {NSMutableArray * temp = [[NSMutableArray alloc] init]; NSString * letterString = [[[msg_array objectAtIndex: i] objectForKey: @ "Merchant_Name"] substringToIndex: 1]; если (![tempFirstLetterArray containsObject: [letterString uppercaseString]]) {[tempFirstLetterArray addObject: letterString]; [temp addObject: [[msg_array objectAtIndex: i] objectForKey: @ "Merchant_Name"]]; }}} ' – Krunal

0

Я сделал то же самое для контактной информации и других вещей, как, что с помощью FKRSearchBarTableViewController см ссылки и ниже мой код FKRSearchBarTableViewController

- (id)initWithSectionIndexes:(BOOL)showSectionIndexes withDataSource:(NSArray*) dataSource withControllerId:(int) ControllerId forGroup:(int)groupId 
{ 
if ((self = [super initWithNibName:nil bundle:nil])) { 
    self.title = @"Search Bar"; 
    NSLog(@"%d",groupId); 
    _groupID = groupId; 
    _controllerId = ControllerId; 

    _showSectionIndexes = showSectionIndexes; 

    _famousPersons = [[NSMutableArray alloc]initWithArray:dataSource]; 
    if (showSectionIndexes) { 
     UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; 

     NSMutableArray *unsortedSections = [[NSMutableArray alloc] initWithCapacity:[[collation sectionTitles] count]]; 
     for (NSUInteger i = 0; i < [[collation sectionTitles] count]; i++) { 
      [unsortedSections addObject:[NSMutableArray array]]; 
     } 
     if(ControllerId == 5) 
     { 
     for (Person *personName in self.famousPersons) { 

       // NSInteger index = [collation sectionForObject:[personName objectForKey:@"FirstName"] collationStringSelector:@selector(description)]; 
      NSLog(@"%@",personName.firstName); 
      NSInteger index = [collation sectionForObject:personName.firstName collationStringSelector:@selector(description)]; 
       [[unsortedSections objectAtIndex:index] addObject:personName]; 
     } 
     } 
     else 
     { 
      for (NSDictionary *personName in self.famousPersons) { 

       NSInteger index = [collation sectionForObject:[personName objectForKey:@"FirstName"] collationStringSelector:@selector(description)]; 

       [[unsortedSections objectAtIndex:index] addObject:personName]; 
      } 

     } 

     NSMutableArray *sortedSections = [[NSMutableArray alloc] initWithCapacity:unsortedSections.count]; 
     for (NSMutableArray *section in unsortedSections) { 
      [sortedSections addObject:[NSMutableArray arrayWithArray:[collation sortedArrayFromArray:section collationStringSelector:@selector(description)]]]; 
     } 

     self.sections = [NSMutableArray arrayWithArray:sortedSections]; 

    } 
0

, чтобы сделать список более динамичным, решение должно быть

// given NSArray names = your full list of name 
//  NSArray indexes = your list of index 

NSMutableArray *nameSections = [NSMutableArray arrayWithCapacity:26]; 
NSMutableArray *filteredIndexes = [NSMutableArray arrayWithCapacity:26]; 

for (NSString *index in indexes) { 

    NSPredicate *predicate = [NSPredicate predicateWithFormat: 
           @"SELF beginswith[c] %@",index]; 
    NSArray *filterNames = [names filteredArrayUsingPredicate:predicate]; 

    if(filterNames.count>0){ 
     [nameSections addObject:filterNames]; 
     [filteredIndexes addObject:index]; 
    } 
} 


NSLog(@"filteredIndexes %@",filteredIndexes); 
NSLog(@"nameSections %@",nameSections); 


numOfSection = nameSections.count 
numOfRow = [[numOfSection indexOfObject:section]count]; 
name = [[numOfSection indexOfObject:section]] indexOfObject:row]; 


// print log 
//given indexes array a~z 

names (
"a_string", 
"a_string2", 
"b_string", 
"b_string2" 
) 

filteredIndexes (
a, 
b 
) 

nameSections (
    (
    "a_string", 
    "a_string2" 
    ), 
    (
    "b_string", 
    "b_string2" 
    ) 
) 
Смежные вопросы