从UITableViewCell中删除对象

时间:2016-11-04 14:05:46

标签: ios uitableview

我继承了这个应用程序(我就像六人加一个代理人,他们的手指在馅饼中,所以它很好地适应了一个大泥球)我们正在添加的部分增强功能是编辑功能某些行。只有在用户触摸UINavigationItem按钮时,才能编辑这些行。

我所做的是将BOOL设置为属性(bCanEdit),当用户触摸按钮时,在选择器中我将此标志从NO更改为YES。然后我在表上调用reloadData。在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;
            }

这适用于添加我需要的文本字段供用户编辑,但是当用户取消该过程时会出现问题。我需要将表格单元格返回到静态状态。

这部分代码适用于删除文本字段:

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

但是在用户滚动表格之前,底层的cell.detailTextLabel不会出现。我尝试以编程方式滚动表,但这不起作用。

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

问题只是刷新单元格视图。

我的建议是扩展UITableViewCell,将逻辑放在其中的函数中

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

并在其内部将您当前使用的逻辑放在cellForRowAtIndexPath中,同时添加反向操作(如果需要,删除文本字段)

最后,当你想改变它的状态时,只需在索引路径上调用给定显示单元格的函数。

或者如果您没有单元格索引,只需刷新所有可见的索引,如果不是问题:

NSArray *paths = [tableView indexPathsForVisibleRows];
相关问题