버튼이있는 셀을 삭제하려고합니다.reloadRowsAtIndexPath를 사용하여 행을 업데이트하는 방법
다음은 셀의 구현입니다.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0.f, 0.f, 30.f, 30.f);
[btn addTarget:self
action:@selector(btnTouched:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
cell.tag = indexPath.row;
}
return cell;
}
- (void)btnTouched:(UIButton *)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
[_myMutableArray removeObjectAtIndex:sender.tag];
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationLeft];
[_tableView endUpdates];
}
버튼을 다시로드 할 때까지 제대로 작동합니다. 삭제 후에 버튼이 다시로드되지 않기 때문에 좋은 색인은 삭제되지 않습니다. 메서드 다음에 visible indexPath
에 대해 reloadRowsAtIndexPaths:withRowAnimation:
을 넣으려고했는데 아주 잘 작동합니다. 그러나 삭제 애니메이션은 reloadRows 애니메이션에 의해 추악 해졌습니다 (비록 NO로 설정하더라도). 그래서 reloadCellsAtIndexPath없이 셀을 다시로드하는 방법이 있습니다. 또는 태그를 사용하지 않고 행을 삭제하십시오.
Thx 많이 있습니다.
매우 똑똑한 방식으로 작동했습니다. 정말 고마워 – klefevre