从中删除数据后,从自定义UITableViewCell中删除按钮

时间:2013-12-19 10:43:40

标签: ios iphone objective-c uitableview

我有一个自定义UITableViewCell,其中还包含自定义UIButton作为子视图。该按钮显示另一个图像,具体取决于UITableViewCell.textLabel.text - 数据。

当我现在删除一行时,数据会被删除并向上移动一行。问题在于,图像保持不变。所以,图像现在重叠了。见这里

image

图像应为蓝色或红色,而不是两者。你也可以看到,它有点厚。那是因为行被删除了,它包含的数据(和数组中的数据)而不是图像。我正在使用自定义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

3 个答案:

答案 0 :(得分:0)

似乎问题是,我总是使用相同的CellIdentifier。现在我把它设置为零并且它有效。

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

答案 1 :(得分:0)

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

在你的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];

答案 2 :(得分:0)

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