0

I have created a UITableView with custom cell & stored name,no,pincode in to these cell.

Here is my Code for array:-

for (int i =0; i<[tempArr count]; i++)
    {
        NSString *rawData = [tempArr objectAtIndex:i];

        if (rawData !=nil)
        {
            Persons *newPerson = [[Persons alloc]init];

            NSArray *data = [rawData componentsSeparatedByString:@"\t"];

            newPerson.name = [NSString stringWithFormat:@"%@",[data objectAtIndex:0]];
            newPerson.no = [[data objectAtIndex:1] integerValue];
            newPerson.pincode = [[data objectAtIndex:2] integerValue];

            [allPersons addObject:newPerson];
        }
    }

Here is my Customcell.h

@interface Customcell : UITableViewCell

@property(weak) Persons* person;

@end

UITableView Datasrouce method:-

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Customcell *cell = [tblStations dequeueReusableCellWithIdentifier:@"personCell"];

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.person = filteredContentList[indexPath.row];
        [cell.textLabel setText:cell.person.name];
    }
    else
    {
        cell.person = allPersons[indexPath.row];
        [cell.textLabel setText:cell.person.name];
    }

    return cell;
}

How do i create Section & index list for all names from A to Z & give title by cell.textLabel.text?

I am following This Tutorial but it has static keys & names added to NSDictionary,NSArray.

In my example i do not know how many names starting with same letter can come in the array. i am also using UISearchDisplayController for search person name.

I want to add number of sections & title for those sections by names that is in the array or cell.textLabel.text dynamically.

i do not know about UISearchDisplayController that these sections & index list will be displaying in UISearchDisplayController so i do not want these sections & index list while searching.

Jaymin Raval
  • 205
  • 2
  • 19
  • Maybe this answer might help solve your problem? http://stackoverflow.com/questions/23410353/search-bar-and-search-display-controller-in-table-view/23452670#23452670 – andrewbuilder Mar 17 '15 at 22:15
  • @andrewbuilder no, i have already added search functionality but my array allPersons contains many names with same letters at first like Andew,Andy,Aerial,Bony,Biska,Vincent,Vonboy,Zed,Zion etc... so i can't use that array for numberofsections. I need to give number of sections count that has no duplicate letters in that array which is in allPersons(name). – Jaymin Raval Mar 17 '15 at 22:26
  • To populate your `numberOfSections` custom method, create a separate `NSSet` local variable based on your array and return that value. – andrewbuilder Mar 17 '15 at 22:31
  • @andrewbuilder but checking for every name in array & creating another dictionary with number of sections sound ridiculous to me because i have to add number of names in the dictionary that starts with letter, e.g. A= 26,B=10,C=5 etc by running for loop for every name & A to Z. – Jaymin Raval Mar 17 '15 at 23:12
  • Read carefully... I suggested you create a set, not a dictionary. Two very different concepts. A set contains only one of each occurrence. So if your array has 26 items that start with the letter A, the corresponding set will contain one letter A. – andrewbuilder Mar 17 '15 at 23:14
  • Just did, see answer I loaded. Read about `NSSet` in the [Apple documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/index.html) – andrewbuilder Mar 17 '15 at 23:38

1 Answers1

0

You need to spend a little more time trying to make your questions more clear.

Include a custom implementation of the necessary UITableView data source and delegate methods...

NOTE my assumption that your variable allPersons is an NSMutableArray.

NOTE these do not include for your search results data sets!

Return an NSInteger for the number of sections in your UITableView...

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSSet *setData = nil;
    NSInteger integerData = 0;

    setData = [NSSet setWithArray:allPersons];
    integerData = [setData count];

    return integerData;
}

UPDATE

Return an NSString for section header titles...

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSOrderedSet *setData = nil;
    NSString *stringData = nil;

    setData = [NSOrderedSet orderedSetWithArray:allPersons];
    stringData = [[setData allObjects] componentsJoinedByString:@" "];

    return stringData;      
}

...plus others if I have the time...

andrewbuilder
  • 3,629
  • 2
  • 24
  • 46
  • i am sorry for making it confusing but i have done as you said but shall i pass this set for titleForHeaderInSection & sectionIndexTitlesForTableView as well, so that i can see title with those unique letters from NSSet & in Indexlist.? – Jaymin Raval Mar 17 '15 at 23:40
  • No. Each needs a separate implementation of the appropriate custom method. You really need to do some more work attempting to understand how a `UITableView` works. There is a lot of information available online. I don't have time to write up all the methods for you now, but may be able to do so later. – andrewbuilder Mar 17 '15 at 23:42