0

I am pulling out some language sentences (in different languages) and displaying them in TableView (max 5 rows each row) and when user taps on any row, then I navigate user to a new screen and display full text there.

The problem that i am running into is, it is taking too much time to convert the characters to be visible properly on device.

I wrote the following code to convert the json text for each row:

NSString *msgDesc = [myContentsArray objectAtIndex:indexPath.row];

char const *cStr = [msgDesc cStringUsingEncoding:NSISOLatin1StringEncoding];

msgDesc = [NSString stringWithCString: cStr encoding:NSUTF8StringEncoding];

Thanks for the help.

Regards,

Reno Jones

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
Reno Jones
  • 1,979
  • 1
  • 18
  • 30

1 Answers1

2

Since conversion is taking too long, you should move it out from your tableView:cellForRowAtIndexPath: method into the code that pulls the data out from its data source, do the conversion there, and store it for future use.

Add NSMutableArray *myContentsArrayConverted to your class, then convert everything into it, and use in your tableView:cellForRowAtIndexPath: instead of performing the conversion each time you must display your string:

for (int i = 0 ; i != myContentsArray.count ; i++) {
    NSString *msgDesc = [myContentsArray objectAtIndex:i];
    char const *cStr = [msgDesc cStringUsingEncoding:NSISOLatin1StringEncoding];
    [myContentsArrayConverted addObject:[NSString stringWithCString: cStr encoding:NSUTF8StringEncoding]];
}

Now you can replace the slow code with the much faster

NSString *msgDesc = [myContentsArrayConverted objectAtIndex:indexPath.row];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Well, I thought about doing it, but never tried the same, however, I was doing in heightForRowAtIndexPath and was trying to replace this formatted string in my actual json for that particular row. Hence, next time whenever I will fetch from array, it gives me formatted one. is there any other way to display different language characters? other than allocing an nsstring and passing cstring to it. – Reno Jones Mar 05 '13 at 21:24
  • @RenoJones Well, another alternative is to [go through `NSData`](http://stackoverflow.com/a/8314908/335858), but I doubt that it would give you much of an improvement. When you calculate the height, do you call the `sizeWithFont:` method repeatedly? If you do, you may want to cache the number returned by it, because the call is not too speedy either, if I remember it correctly. – Sergey Kalinichenko Mar 05 '13 at 21:30
  • Yes, I am caching the height in the json Array itself, reusing the stuff with the best of my ability. I believe NSdata+NSString method is similar to the NSString+CString.. – Reno Jones Mar 05 '13 at 21:35