【发布时间】: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