ios – 我可以在moveRowAtIndexPath中调用reloadRowsAtIndexPaths …吗?

在我的UITableView中,我有一个特定的单元格(A),其外观取决于同一部分中所有其他单元格的顺序.因此,每当用户将单元格拖动到表视图中的新位置时,我需要更新单元格(A)的内容.

在我看来,最好的方法是在一个方法中调用reloadRowsAtIndexPath:@ [indexPathOfCellA],该方法在完成表格视图重新排序后调用,类似于didMoveRowFromIndexPath:toIndexPath:.不幸的是,没有这样的方法.

因此我尝试在moveRowFromIndexPath:toIndexPath:方法的末尾调用reloadRowsAtIndexPath:@ [indexPathOfCellA]:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSString *element = [self.dataSource objectAtIndex:sourceIndexPath.row];
    [self.dataSource removeObjectAtIndex:sourceIndexPath.row];
    [self.dataSource insertObject:element atIndex:destinationIndexPath.row];
    [self updateInfoCellInTableView:tableView];
}

用相应的方法:

- (void)updateExampleCellInTableView:(UITableView *)tableView {
    UInt32 infoCellRow = [tableView numberOfRowsInSection:0] - 1;
    NSIndexPath *indexPathForInfoCell = [NSIndexPath indexPathForRow:infoCellRow inSection:0];
    [tableView reloadRowsAtIndexPaths:@[indexPathForInfoCell] withRowAnimation:UITableViewRowAnimationAutomatic];
}

这不能正常工作.

当将单元格(A)拖动到新位置(B)时,该位置(B)处的旧单元格首先向上或向下移动以便为单元格(A)腾出空间.但当我抬起手指确认重新排序的细胞(B)移回其原始位置(B)时,两个细胞重叠. (我可以看到,因为细胞的背景是半透明的.)

我的假设是不允许从moveRowAtIndexPath:toIndexPath:中调用reloadRowsAtIndexPath:.但是,我在Apple的Developer Docs中找不到这样的规则.

这个假设是否正确?重新排序表视图后如何更新特定单元格?

最佳答案 我已经研究过这个,这是一个简单的例子.请仔细阅读一次.它可能会帮助你.

- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.

   tblData=[[UITableView alloc]init];
   [tblData setDataSource:self];
   [tblData setDelegate:self];
   [tblData setFrame:CGRectMake(0, 75, 320, 430)];
   [self.view addSubview:tblData];
   [tblData setEditing:YES animated:NO];

   mutArr=[[NSMutableArray alloc]init];
   [mutArr addObject:@"Object 1"];
   [mutArr addObject:@"Object 2"];
   [mutArr addObject:@"Object 3"];
   [mutArr addObject:@"Object 4"];
   [mutArr addObject:@"Object 5"];
   [mutArr addObject:@"Object 6"];
   [mutArr addObject:@"Object 7"];
   [mutArr addObject:@"Object 8"];
   [mutArr addObject:@"Object 9"];
   [mutArr addObject:@"Object 10"];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return mutArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell=[[UITableViewCell alloc]init];
   cell.textLabel.text=[mutArr objectAtIndex:indexPath.row];

   return cell;

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
   return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{

   NSString *element = [mutArr objectAtIndex:sourceIndexPath.row];
   [mutArr removeObjectAtIndex:sourceIndexPath.row];
   [mutArr insertObject:element atIndex:destinationIndexPath.row];

}

- (void)tableView:(UITableView *)tableView willBeginReorderingRowAtIndexPath:(NSIndexPath *)indexPath
{
   previousPosition=(int)indexPath.row;
}


- (void)tableView:(UITableView *)tableView didEndReorderingRowAtIndexPath:(NSIndexPath *)indexPath
{

   UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
   cell.textLabel.text=[NSString stringWithFormat:@"This cell moved from :%d to :%ld position",previousPosition,(long)indexPath.row];

}

这里“previousPosition”是一个整数,它被声明为全局变量.

点赞