How does IOS cycle through cells

Time:2022-1-11

First of all, it is not advisable to traverse all cells

For example, it can also be said how to develop multi condition input view and condition selection view.

The requirements are shown in the figure. The example only lists a few information, and there may be many in the actual project. Here, the information entered by the user needs to be assembledSave together。 Most people who haven’t touched this demand will consider using itUITableView, after use, the problem comes

How does IOS cycle through cells

Question: how to get the user’s input after input?

First inUITableViewCellof Attributes declared in H file

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UITextView *textView;

Loop through on commit to get:

- (void)save{
    for (int i = 0; i < 6; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        NSString *stirng = cell.textFiled;
    }
}

If you did, you were wrong from the beginning becausecellForRowAtIndexPathOnly cells within the visible range of the screen can be obtained. Once the input conditions are increased and beyond the screen, this cell cannot be obtained.

And you still need to solvereusingNo problem. You won’t search this kind of article. I stepped on the pit. Don’t use it hereNot reuseCell to solve the problem.

So what is the correct posture?

Mode 1

Two way data binding, namely MVVM. If your project uses MVVM, you won’t encounter this problem.

Of course, it doesn’t matter if your project is not based on MVVM, because reactivecocoa
KVO, notification, block, delegate and target action can all be used for data communication. Here we choose the simplest block because it is suitable for projects with various architecture modes and is relatively simple to use.

For non MVVM projects, you do not need to create a VM in use. You only need to create a VM in thecellA block is defined in. When the content of the input box changes, the input content is returned. At the callback place of the block, the data model can be updated. It is also convenient to judge. The circular data model can be used (array and map can be used as convenient)

Mode II

Customize a view. The style is the same as the previous cell. It is used according to enumeration judgment when creatingUITextFieldstillUITextView

typedef NS_ENUM(NSInteger, InputCellType){
    InputCellType_TextField  = 1,
    InputCellType_TextView   = 2
};

- (instancetype)initWithFrame:(CGRect)frame inputType:(InputCellType)inputType

After that, the loop is added to theUIScrollView:

- (void)addContentView{
    [self.inputCellArray removeAllObjects];
    InputCellView *lastCellView;
    for (int i = 0; i < 6; i++) {
        InputCellView *cellView;
        if(i == 0){
            cellView = [[InputCellView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.width, InputCellHeight_TextField) inputType:InputCellType_TextField];
        }else if(i == 5){
            cellView = [[InputCellView alloc] initWithFrame:CGRectMake(0, lastCellView.bottom, self.scrollView.width, InputCellHeight_TextView) inputType:InputCellType_TextView];
        }
        else{
            cellView = [[InputCellView alloc] initWithFrame:CGRectMake(0, lastCellView.bottom, self.scrollView.width, InputCellHeight_TextField) inputType:InputCellType_TextField];
        }
        
        lastCellView = cellView;
        [self.scrollView addSubview:cellView];
        [self.inputCellArray addObject:cellView];
    }
        
    self.scrollView.contentSize = CGSizeMake(self.view.width, lastCellView.bottom);
}

It is more convenient to obtain data:

- (NSString *)getInputCellContentWithIndex:(NSInteger)index{
    InputCellView *cellView = self.inputCellArray[index];
    if(cellView.inputType == InputCellType_TextField){
        return cellView.textField.text;
    }else{
        return cellView.textView.text;
    };
}

Recommended Today

Smartbi: what is data mining technology and what benefits can it bring to enterprises

In today’s era of big data, there are more and more data sources, including websites, enterprise applications, social media, mobile devices and the Internet of things, as well as more and more data generated by the Internet of things. For enterprises, how to obtain real business value from these data becomes more and more important,data […]