我有自定義單元格的表格視圖。單元格填充了我的數據。 現在我想讓用戶重新排列行。我已經實現了這些方法,但是在拖拽重新排序單元格時,我可以看到它正在嘗試執行但不能移動到任何地方的顯示。它像10個像素一樣移動,就好像它將重新排列但回到其位置。如何使用自定義單元重新排序行?如何用自定義單元格重新排列UITableView?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.dataSource removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
}
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (self.mytableView.editing)
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
id stringToMove = [self.dataSource objectAtIndex:sourceIndexPath.row];
[self.dataSource removeObjectAtIndex:sourceIndexPath.row];
[self.dataSource insertObject:stringToMove atIndex:destinationIndexPath.row];
}
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section != sourceIndexPath.section)
{
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}
你應該認真對待你的代碼縮進! – JustSid 2012-08-09 07:17:02
xcode的代碼縮進很好,只要在這裏複製,它就搞砸了。所以任何想法爲什麼重新排列domenst發生? – 2012-08-09 08:23:14