2013-12-19 62 views
0

我有一個自定義UITableViewCell還保留作爲子視圖的自定義UIButton。該按鈕顯示另一個圖像,取決於UITableViewCell.textLabel.text-數據。刪除自定義UITableViewCell中的一個按鈕後,從它刪除數據

當我現在刪除一行時,數據被刪除並向上移動一行。存在的問題是,圖像保持不變。所以,圖像現在疊加在一起。看到這裏

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

正如你可以看到我從保存的數據,然後重新加載我有tableviews的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

+0

使用deleteRowsAtIndexPaths也已經更新了答案,請遵循 –

回答

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

通過使用'nil'小區標識,不使用可重複使用的電池,和'dequeueReusableCellWithIdentifier:'總是返回'nil',所以你將創建一個新的細胞每時間。 –

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

,而不是從細胞從刪除數據可以刪除整排。

+0

plz發佈一些代碼並進行更多解釋 – Backtrack

相關問題