views:

2307

answers:

3

I have a table view in which I'm using sectionIndexTitlesForTableView to display an index. However, when I scroll the table, the index scrolls with it. This also results in very slow refreshing of the table. Is there something obvious I could be doing wrong? I want the index to remain in place on the right while the table scrolls. This is the code I'm using for the index titles:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    [tempArray addObject:@"A"];
    [tempArray addObject:@"B"];
    [tempArray addObject:@"C"];
    [tempArray addObject:@"D"];
...

    return tempArray; 
}
+1  A: 

I would avoid creating a new NSMutableArray and releasing it every time. Try creating those on viewDidLoad or the class constructor and just reference the pre-built array on sectionIndexTitesForTableView.

If you are not manipulating the array at all, you probably don't need the overhead of an NSMutableArray at all. Try switching it to a plain old NSArray by using the arrayWithObjects static autorelease constructor.

That should speed things up for you.

slf
Thanks for the advice. I now have this:- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { NSArray *tempArray = [NSArray arrayWithObjects:@"A",@"B",@"C", nil]; return tempArray; }But it doesn't help at all (table content is not updated until scrolling is finished). I think the main problem is that the index is scrolling along with the table. This is obviously incorrect - it should stay still as the table cells scroll. Any idea why this could be happening?
I'm not sure what "index is scrolling along with the table" means. You are still creating the array every time that method is called with that code.
slf
If you are using sectionIndexTitlesForTableView make sure you are also implementing tableView:sectionForSectionIndexTitle:atIndex: and tableView:numberOfRowsInSection:
slf
+2  A: 

You really should be creating the index list somewhere else (say, in your table controller's init or loadView methods) and retaining it as an instance variable for later use. Then in sectionIndexTitlesForTableView you only have to return that ivar. If it isn't a property with a retain attribute then make sure you retain it when created so it sticks around (and release it in dealloc).

An easy way to create it is:

self.alphabetIndex = [NSArray arrayWithArray:
   [@"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|#"
      componentsSeparatedByString:@"|"]];

The actual letters would have to change depending on the language locale setting but this way it's a bit easier to localize.

You definitely don't want to be creating that temp array each time because it's going to get called a lot.

As far as the index scrolling away it may be related to your returning a new array each time. Try the above first and if it doesn't solve the problem then you may want to tweak the value for the table's sectionIndexMinimumDisplayRowCount property and see if it makes any difference.

Ramin
Yes, I suspected that creating the array each time was not a good idea, and was already implementing a solution like the one you suggested. I am just returning alphabetArray now. However, even after doing that, the index still scrolls away, then pops back into place only after scrolling is finished. This is baffling. Why in the world would the index scroll? Tapping or swiping on the index itself scrolls the table properly.I tried setting myTableView.sectionIndexMinimumDisplayRowCount to some different values (3,10,20) - didn't make any difference.
Maybe if you try setting it to the number of items in the alphabetIndex (i.e. 27) it'll keep it in place. Worth a try.
Ramin
Currently the table has less than 27 rows, so setting it to 27 causes the index to not be displayed. Which means of course that the table scrolls and displays perfectly. But still, whenever I display the index the table scrolling doesn't render until the scrolling stops.
Hmm. I suggested 27 because of 26 letters of alphabet plus '#' in the alphabetIndex example. If it still scrolls up with the right count of the number of elements in the array, then suggest you use up an Apple Developer Tech Support credit and see if they can help by looking at the code.
Ramin
A: 

You definitely don't want to be creating that temp array each time because it's going to get called a lot.

Why would that method be "called a lot"?

Wouldn't I really just need to built this index once... and wouldn't sectionIndexTitlesForTableView also be called only once?

(Unless I change the table.)

Susanna