简体   繁体   English

从UITableViewCell删除对象

[英]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. 这些行仅在用户触摸UINavigationItem按钮后才可编辑。

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. 我所做的是将BOOL设置为属性(bCanEdit),当用户触摸按钮时,在选择器中,我将此标志从NO更改为YES。 I then call reloadData on the table. 然后,我在表上调用reloadData。 In the cellForRowAtIndexPath I have this code: 在cellForRowAtIndexPath中,我有以下代码:

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. 但是底层的cell.detailTextLabel直到用户滚动表格后才会出现。 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 我的建议是扩展UITableViewCell,将逻辑放入其中的函数中

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) 并在其中放入您当前在cellForRowAtIndexPath使用的逻辑,还添加了反向操作(如果需要,请删除文本字段)

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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