If you create ComboBox in Cell in UITableView, it will be reversed for some reason...

Asked 2 years ago, Updated 2 years ago, 142 views

This is the state at first.

This is what happens when you click Option1.

Press Option 1 again to close Combobox, but click again to see this image.

As shown in the image above, every time I select Cell, the display is reversed.

This is UITableView's DidSelectRowAtIndexPath

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
    {
    switch([indexPath section]){
    case1: {

        switch([indexPathrow]){
            case0:
            {
                DropDownCell*cell= (DropDownCell*) [tableView cellForRowAtIndexPath:indexPath];

                NSIndexPath* path0 = [NSIndexPathindexPathForRow: [indexPathrow]+1 inSection: [indexPath section]];
                NSIndexPath* path1 = [NSIndexPathindexPathForRow: [indexPathrow] + 2 inSection: [indexPath section]];
                NSIndexPath* path2 = [NSIndexPathindexPathForRow: [indexPathrow] + 3 inSection: [indexPath section]];

                NNSArray* indexPathArray= [NSArray arrayWithObjects: path0, path1, path2, nil ];

                if ([cell isOpen])
                {
                    [cell setClosed];
                    dropDown1Open=[cell isOpen];

                    tableView deleteRowsAtIndexPath:indexPathArray withRowAnimation:UITableViewRowAnimationTop;
                }
                else
                {
                    cell setOpen;
                    dropDown1Open=[cell isOpen];

                        tableView insertRowsAtIndexPath:indexPathArray withRowAnimation:UITableViewRowAnimationTop;

                }

                break;
            }
            default:
            {
                dropDown1 = [[[tableView cellForRowAtIndexPath:indexPath] textLabel text];

                NSIndexPath* path = [NSIndexPathindexPathForRow:0 inSection: [indexPath section]];
                DropDownCell*cell= (DropDownCell*) [tableView cellForRowAtIndexPath:path];

                [[cell textLabel] setText:dropDown1];

                NSIndexPath* path0 = [NSIndexPathindexPathForRow: [pathrow]+1 inSection: [indexPath section]];
                NSIndexPath* path1 = [NSIndexPathindexPathForRow: [pathrow] + 2 inSection: [indexPath section]];
                NSIndexPath* path2 = [NSIndexPathindexPathForRow: [pathrow] + 3 inSection: [indexPath section]];

                NNSArray* indexPathArray= [NSArray arrayWithObjects: path0, path1, path2, nil ];

                [cell setClosed];
                dropDown1Open=[cell isOpen];

                tableView deleteRowsAtIndexPath:indexPathArray withRowAnimation:UITableViewRowAnimationTop;

                break;

            }
        }

    }

}

[tableView selectRowAtIndexPath:indexPath animated:YES]
}

I would like to add cellForRowAtIndexPath just in case.

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell* cell= [tableView requestReusableCellWithIdentifier:@"Cell";
if(cell==nil){

    switch([indexPath section]){
        case0:{
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell";

            cell.textLabel.text=@"first";
            break;
        }
        case1: {
            switch([indexPathrow]){
                case0:{

                    DropDownCell* cell= (DropDownCell*) [tableView requestReusableCellWithIdentifier:@"DropDownCell";

                    if(cell==nil){
                        NSLog (@"New Cell Made");

                        NSArray*topLevelObjects=[NSBundle mainBundle] loadNibNamed:@"DropDownCell" owner:nil options:nil];

                        for (id currentObject in topLevelObjects)
                        {
                            if([currentObject isKindOfClass:[DropDownCell class]])
                            {
                                cell=(DropDownCell*) currentObject;
                                break;
                            }
                        }

                        if(dropDown1Open){
                            cell setOpen;
                        }

                        [[cell textLabel] setText:dropDown1];
                    }

                    // Configure the cell.
                    return cell;

                    break;
                }
                default: {
                    UITableViewCell* cell= [tableView requestReusableCellWithIdentifier:@"Cell";

                    if(cell==nil){
                        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell";
                    }

                    NSSstring* label = [NSSstring stringWithFormat: @ "Option %ld", [indexPathrow]];

                    [cell textLabel] setText:label;

                    // Configure the cell.
                    return cell;

                    break;
                }
            }

            break;
        }

    }
}
return cell;
}

I'm so into it that I can't move forward easily.
I look forward to hearing from you.

ios objective-c uitableview

2022-09-30 11:34

1 Answers

Probably
https://github.com/floriankrueger/iOS-Examples--UITableView-Combo-Box
I think it was coded by referring to the sample in .

If you compare it with the sample, you'll find out right away.
cellForRowAtIndexPathThe first two lines of your coding

UITableViewCell*cell=[tableView requestReusableCellWithIdentifier:@"Cell";
if(cell==nil){

is not in the sample.
I think this part you added is the cause of the defect.

dequeReusableCellWithIdentifier is a method to reuse previously used cells. Cells used in different table rows may be reused.
Your coding returns the acquired cells as a return of the method without doing anything, so
I returned the cell that I used to display Option 3 as a cell for displaying Option 1.
If you update the table display, the display order will be incorrect.

After retrieving the previously used cell in dequeReusableCellWithIdentifier,
The contents of the cell must be updated to display the lines specified in indexPath.
The processing was originally included in the reference sample coding.

in default processing of case statement
NSString*label=[NSString stringWithFormat:@"Option%ld", [indexPathrow]];
[cell textLabel] setText:label;

It says.
In other words, the two lines you added are unnecessary, and if you remove them,
The default action for the case statement should work to ensure proper display.


2022-09-30 11:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.