UITextField slow to initialize?
I am putting about 100 UITextFields programmatically into a UIScrollView for an elaborate data entry app (don't ask....bletch) and I am finding that it is taking too long to generate all of these UITextFields... something li开发者_StackOverflowke 4 seconds.
Is there a better or quicker way to programmatically make so many user interface objects?
For instance would it be wiser to load a Xib/Nib and try to modify it as needed?
Thanks.
Have you thought about just creating a couple of UITextField
s and reusing them once they're offscreen just as the UITableView
does with cells?
Ouch, 100 textfields as data entry are harsh. But i know the problem when the client insists on a very dumb idea.
Back to topic: The problem is not the actual generation of your UITextFields
. It is the way cocoa touch handles views. Views are terribly slow. Your idea with nibs is even worse, because they are in fact even slower.
The only way to avoid that is to simply not draw (add) many views. Just add the views which are currently in the visible area of the scrollview and remove the others.
Cocoa touch has actually already a very good control for that, it is called UITableView
. ;)
Apple created the dequeue/reuse pattern in the UITableView exactly due those reasons.
So my suggestions for you is to use a tableview instead of a scrollview with custom cells (to avoid separators, etc.) and dequeue/reuse these cell and fill them with UITextFields which you store inside an array somewhere.
my first idea is to write a timer and load them 1 by 1 (or more) in small steps so that the UI doesn't freeze (it will still take 4 sec but at least user can input into into the loaded text fields)
here is how to make a timer
精彩评论