我有一個自定義UITableViewCell
還保留作爲子視圖的自定義UIButton
。該按鈕顯示另一個圖像,取決於UITableViewCell.textLabel.text
-數據。刪除自定義UITableViewCell中的一個按鈕後,從它刪除數據
當我現在刪除一行時,數據被刪除並向上移動一行。存在的問題是,圖像保持不變。所以,圖像現在疊加在一起。看到這裏
的形象應該是藍色或紅色,而不是兩個。你也可以看到,它有點厚。這是因爲行被刪除,它包含的數據(在數組中)而不是圖像。我正在使用自定義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
使用deleteRowsAtIndexPaths也已經更新了答案,請遵循 –