简体   繁体   中英

Removing Objects From UITableViewCell

I've inherited this app (and I am like person six plus one agency to have their fingers in the pie so it's barreling nicely to a big ball of mud) and part of the enhancements we are adding is the ability to edit certain rows. These rows can only be editable once a user touches a UINavigationItem button.

What I've done is set a BOOL as a property (bCanEdit) and when the user touches the button, in the selector I change this flag from NO to YES. I then call reloadData on the table. In the cellForRowAtIndexPath I have this code:

UITableViewCell *cell = nil;

    cell.accessoryType = UITableViewCellAccessoryNone;

    if ([indexPath section] == kStaticSection) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:staticCellId];

        switch ([indexPath row])
        {
            case kPhoneCell:
            {
                cell.textLabel.text = @"Phone:";

                    if (!self.bNeedsEdit) {


                        if (cell.contentView.subviews.count > 0) {
                            for(id thisItem in cell.contentView.subviews) {
                                if ([thisItem isKindOfClass:[UITextField class]]) {
                                    [thisItem removeFromSuperview];
                                }                             }
                        }

                        cell.detailTextLabel.text = self.customer.formattedPhone;

                    } else {

                        UITextField* txtPhone = [[UITextField alloc] initWithFrame:CGRectMake(cell.detailTextLabel.frame.origin.x, cell.detailTextLabel.frame.origin.y-4.0, 400.0, 24.0)];
                        txtPhone.text = self.customer.formattedPhone;
                        txtPhone.borderStyle = UITextBorderStyleRoundedRect;
                        txtPhone.tag = 92;
                        txtPhone.delegate = self;
                        txtPhone.keyboardType = UIKeyboardTypeNumberPad;
                        [cell.contentView addSubview:txtPhone];

                        cell.detailTextLabel.text = @"";

                    }


                break;
            }

this works fine for adding the textfields I need for the user to edit but the problem comes with when a user cancels the process. I need to return the table cells to their static state.

This part of the code does work for removing the text field:

if (cell.contentView.subviews.count > 0) {
                            for(id thisItem in cell.contentView.subviews) {
                                if ([thisItem isKindOfClass:[UITextField class]]) {
                                    [thisItem removeFromSuperview];
                                }                             }
                        }

but the underlying cell.detailTextLabel does not appear until the user scrolls the table. I tried programmatically scrolling the table but that didn't work.

Any ideas would be appreciated.

The problem is just to refresh the cell view.

My suggestion would be to extend UITableViewCell, put the logic inside a function in it like

updateViewWithData:(id)whatever-object-or-info 

and inside it put the logic you currently use in the cellForRowAtIndexPath adding also the reverse operations (remove the textfield if needed)

finally when you want to change it's state just call the function for the given shown cell at index path.

or if you don't have a cell index, just refresh all the visible ones if is not an issue:

NSArray *paths = [tableView indexPathsForVisibleRows];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM