【问题标题】:Remove a button from a custom UITableViewCell after deleting the data from it删除数据后从自定义 UITableViewCell 中删除按钮
【发布时间】:2013-12-20 15:37:34
【问题描述】:

我有一个自定义的UITableViewCell,它还拥有一个自定义的UIButton 作为子视图。该按钮显示另一个图像,取决于UITableViewCell.textLabel.text-data。

当我现在删除一行时,数据将被删除并向上移动。存在的问题是,图像仍然存在。因此,图像现在正在叠加。看这里

图像应该是蓝色或红色,而不是两者。你也可以看到,它有点厚。那是因为该行被删除并且它包含的数据(和在数组中)而不是图像。我正在使用自定义UITableViewCell 进行自定义滑动删除。滑动手势还允许用户编辑数据。不仅删除它。

这是当我按下删除或编辑按钮时被调用的方法

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
    switch (index) {
        case 0: {
            //stuff for editing
        }
        case 1:{
            NSString *stringIdentifierTemp  = cell.projectCellIdentifier;
            Firebase *firebaseReferenceTemp = [[Firebase alloc] initWithUrl:[NSString stringWithFormat:@"%@/%@", stringRequestUrl, stringIdentifierTemp]];
            if ( self.boolFirstTableView ){
                [dictionaryProjects removeObjectForKey:stringIdentifierTemp];
                [firebaseReferenceTemp updateChildValues:@{@"state": @"canceled"}];
            } else {
                [dictionaryFinishedProjects removeObjectForKey:stringIdentifierTemp];
                [firebaseReferenceTemp removeValue];
            }
            [tableView reloadData];
            [tableViewFinished reloadData];
            break;
        }
        default:
            break;
    }
}

如您所见,我从保存数据的NSMutableDictionary 中删除了数据,然后重新加载了我拥有的表格视图。不过,图像仍在叠加。

在方法- (SWTableViewCell *)tableView:(UITableView *)realTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中,我定义了UITableViewCells,这就是我添加按钮的方式

CustomButton *stateButton           = [CustomButton buttonWithType:UIButtonTypeRoundedRect];
    stateButton.projectButtonIdentifier = tempIdentifier;
    stateButton.frame                   = CGRectMake(5.0f, 5.0f, 44.0f, 44.0f);
    [stateButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [stateButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [stateButton addTarget:self action:@selector(updateState:) forControlEvents:UIControlEventTouchUpInside];
    Firebase *tempRef = [[Firebase alloc] initWithUrl:[NSString stringWithFormat:@"%@/%@", stringRequestUrl, tempIdentifier]];
    [tempRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
        if ( [snapshot.name isEqualToString:@"state"] ) {
            if ( [snapshot.value isEqualToString:@"new"]){
                if ( [important isEqualToString:@"No"] ){
                    [stateButton setBackgroundImage:imageStatusNew forState:UIControlStateNormal];
                } else {
                    [stateButton setBackgroundImage:imageStatusNewRed forState:UIControlStateNormal];
                }
            } else if ( [snapshot.value isEqualToString:@"started"]){
                if ( [important isEqualToString:@"No"] ){
                    [stateButton setBackgroundImage:imageStatusStarted forState:UIControlStateNormal];
                } else {
                    [stateButton setBackgroundImage:imageStatusStartedRed forState:UIControlStateNormal];
                }
            } else if ( [snapshot.value isEqualToString:@"halfway"]){
                if ( [important isEqualToString:@"No"] ){
                    [stateButton setBackgroundImage:imageStatusHalfway forState:UIControlStateNormal];
                } else {
                    [stateButton setBackgroundImage:imageStatusHalfwayRed forState:UIControlStateNormal];
                }
            }
        }
    }];

    [cell addSubview:stateButton];

一个简单的题外话:我知道它应该写成cancelled,但我没有做这些。 :p

【问题讨论】:

  • 使用deleteRowsAtIndexPaths也有更新答案请关注

标签: ios iphone objective-c uitableview


【解决方案1】:

问题似乎是,我总是使用相同的CellIdentifier。现在我把它设置为 nil 就可以了。

SWTableViewCell *cell               = (SWTableViewCell *)[realTableView dequeueReusableCellWithIdentifier:nil];

// Cell
if ( cell == nil ){
    cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil
                                  containingTableView:realTableView leftUtilityButtons:[self leftButtons] rightUtilityButtons:[self rightButtons]];
    cell.delegate = self;
}

【讨论】:

  • 通过使用 nil 单元格标识符,您不会使用可重复使用的单元格,dequeueReusableCellWithIdentifier: 将始终返回 nil,因此您每次都将创建一个新单元格。
【解决方案2】:

当您使用单元格标识符时,不再可见的单元格会被重新使用以节省内存,因此出队时返回的单元格可能已经有一个按钮。

在您的tableView:cellForRowAtIndexPath: 中,如果单元格已经有一个按钮,您应该删除该按钮,您可以通过为其设置标签来做到这一点:

CustomButton *stateButton = ... 
...

stateButton.tag = 12345; // use a tag to identify the button
[[cell viewWithTag:stateButton.tag] removeFromSuperview]; // removes old button if it exists
[cell addSubview:stateButton];

【讨论】:

    【解决方案3】:

    您可以删除整行,而不是从单元格中删除数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      相关资源
      最近更新 更多