簡體   English   中英

從UITableViewCell刪除對象

[英]Removing Objects From UITableViewCell

我繼承了這個應用程序(我很像第六個人和一個代理商,將他們的手指放在餡餅上,這樣就很容易裝進一個大泥球了),我們添加的部分增強功能是可以編輯某些行。 這些行僅在用戶觸摸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直到用戶滾動表格后才會出現。 我嘗試以編程方式滾動表格,但這沒有用。

任何想法,將不勝感激。

問題只是刷新單元格視圖。

我的建議是擴展UITableViewCell,將邏輯放入其中的函數中

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

並在其中放入您當前在cellForRowAtIndexPath使用的邏輯,還添加了反向操作(如果需要,請刪除文本字段)

最后,當您想要更改其狀態時,只需在索引路徑中為給定顯示的單元格調用該函數即可。

或者,如果您沒有單元格索引,則只要沒有問題就刷新所有可見的索引即可:

NSArray *paths = [tableView indexPathsForVisibleRows];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM