Load all cells in UITableView before scrolling

前端 未结 4 2006
面向向阳花
面向向阳花 2020-12-19 04:40

I have a UITableView with 8 cells(sections) in total in which 6 of them contain textFields as subviews and out of 2 one containing button and the other containing text view.

相关标签:
4条回答
  • 2020-12-19 05:18

    It sounds like you might be better off not using a table view at all, but using a scroll view instead. If you really do have a fixed amount of things that you are going to display, you can just lay these out on a scroll view as views (instead of table cells) and your code will get a lot simpler.

    If what you want out of the table view is just the appearance, you can simply use a background image that looks the way you want, and overlay the views and controls over that background. You should be able to do all of this in Interface Builder.

    If you take this approach, the code in your view controller will now be just what you need to handle the text input and button presses, and you don't need to worry about a table view delegate or data source.

    0 讨论(0)
  • 2020-12-19 05:25

    It would make sense that there will be a lot of issues coming from the way you are building the form (which is actually the case) what I've been doing myself when I need to build a such a form is to use build a normal view with all the controls whatever long it is and have that form in a scroll view and set its content size with the size of the view in it. that would let you have more control on how you want that form to look. and I don't think I've seen an app using a tableview for a form entry.

    you can use interface builder to build your form (just cheat on Xcode and make the main view and the scroll vew large enough so u can see everything on the builder, then resize it to the appropriate size when you are done)

    0 讨论(0)
  • 2020-12-19 05:32

    Although this seems to be already solved, what could have done as a "dirty hack" is to make the entire table scroll till bottom before performing any operation. Something like [myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; This way it would be forced to load all the UITableCellView. Remember, this is not preferred way, but can work in case minimum code change is required.

    0 讨论(0)
  • 2020-12-19 05:35

    What happens is that UITableView loads only the visible cells, and all the time that the UITableView is scrolled, the callback cellForRowAtIndexPath: is called. But let me get this straight. Why do you need a UITableView to make a form? And as I can see, you are loading one cell per section... And, you are only initializing the cell. What you should do. Subclass UITableViewCell and create your objects there.

    then call like this:

    cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
    

    Then you can call exactly the controller you want. And you have to control the behavior of your UITableView whenever it is scrolled. like this:

    case 7:
            {
                if (cell == nil) 
                {
                    cell = [[[MyCustomCellEigth alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                    cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];
    
                    /*this should be done in the Custom Cell init method
                    if([cellIdentifierF isEqualToString: eightCellIdentifier])
                    {
                        textField.tag = 107;
                        textField.inputView = self.groupPicker;
                        tagValues = textField.tag;
                        textField.text = reminderInstance.remGroup;
                        NSLog(@"Value = %@",textField.text);
                        [cell.contentView addSubview:textField];
                        [self.fields addObject:textField];
                    }
                    */
                }
                else
                {
                    //Control content
                    ((MyCustomCell*)cell).myTextField.text = @"SOME TEXT";
                }
          }
          break;
    

    About the picker. It probably only works once you have scrolled all table, because it references a textField that will only be added to the Array once the correspondent cell become visible.

    There isn't much more to do. But follow this and you should be fine. First, Subclass UITableViewCell. You can create the controls yourself programmatically or with a xib. This will give you more control over the cells itself. About the picker, instead of creating a array with controls, create it with the values it should have. Once someone has typed something, save it using the UITextFieldDelegate. Hope it helps.

    EDIT:

    You must create a Subclass of the UITableViewCell. On Xcode, create a new file. Select objective-c Class. On the next screen, on the field subclass of, select UITableViewCell and name your cell accordingly. This will create a class that will override the initWithStyle:reuseIdentifier: method. Inside this method, you should put your init code.

    like this:

    #import <UIKit/UIKit.h>
    
    @interface MyCustomCellOne : UITableViewCell
    {
         UITextField *textField;
    
    }
    @end
    
    #import "MyCustomCellOne.h"
    
    @implementation MyCustomCellOne
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
                        textField = [[UITextField alloc] initWithFrame:"YOUR FRAME"];
                        textField.tag = 107;
                        textField.text = @"SOME TEXT";
    
                        [self addSubview:textField];           
        }
        return self;
    }
    
    0 讨论(0)
提交回复
热议问题