我可以在moveRowAtIndexPath中调用reloadRowsAtIndexPaths ...吗?

时间:2015-04-20 09:24:19

标签: ios uitableview reload

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

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

因此,我尝试在reloadRowsAtIndexPath:@[indexPathOfCellA]方法结束时调用moveRowFromIndexPath:toIndexPath:

- (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)时,两个细胞重叠。 (我可以看到,因为细胞的背景是半透明的。)

我的假设是不允许从reloadRowsAtIndexPath:内拨打moveRowAtIndexPath:toIndexPath:。但是,我在Apple的Developer Docs中找不到这样的规则。

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

1 个答案:

答案 0 :(得分:2)

我已经研究过这个,这是一个简单的例子。请仔细阅读一次。它可能对你有帮助。

- (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"是一个整数,声明为全局变量。

相关问题