【发布时间】:2013-11-18 18:39:07
【问题描述】:
我在一个 ViewController 中遇到多个 UITableViews 的问题/理解问题...
我有 4 个 UItableviews。所以我创建了 foreach a: NSObjet<TableViewDelegate/Source>
然后我在 ViewController 中创建了一个 UILongPressGestureRecognizer -Action 并获取我选择的项目并按住以将其拖动到另一个 UITableView
工作正常,我拿到了我的手机,但我怎么能拿着它?有没有办法找出我在哪个 tableview 中删除了 UITableViewCell?
特别感谢 user1966730 (Kristof)
我的新 UIVIEW.h:
@interface MovingCell : UIView
@property(strong,nonatomic)UIImageView *imageViewCell;
@property(strong,nonatomic)UITableViewCell *cell; //This should be later my Custom Cell
@property(strong,nonatomic)NSIndexPath *indexPath;
@property(strong,nonatomic)UITableView* tableView;
-(void)displayCell:(UITableViewCell *)cell inView:(UIView *)mainView;
-(void)bringViewOverSelectedCell:(UIView *)mainView;
@end
UIVIEW.m:
#import "MovingCell.h"
#import <QuartzCore/QuartzCore.h>
#define _Shadow_Space_Height 7.0f
#define _Shadow_Space_Width 7.0f
@implementation MovingCell
@synthesize imageViewCell = _imageViewCell;
@synthesize cell = _cell;
-(void)displayCell:(UITableViewCell *)cell inView:(UIView *)mainView{
self.cell = cell;
//Screenshot of touchedCell
UIGraphicsBeginImageContext(cell.bounds.size);
[cell.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// init imageView for cell snapshop if needed
if (_imageViewCell == nil)
{
_imageViewCell = [[UIImageView alloc] init];
[self addSubview:_imageViewCell];
}
// set current snapshot as image
[_imageViewCell setImage: screenShot];
// calculate position and frame
CGRect viewFrameInMainView = [mainView convertRect:cell.frame fromView:cell.superview];
_imageViewCell.frame = viewFrameInMainView;
viewFrameInMainView.origin.x -= _Shadow_Space_Width;
viewFrameInMainView.origin.y -= _Shadow_Space_Height;
viewFrameInMainView.size.width += _Shadow_Space_Width * 2.0f;
viewFrameInMainView.size.height += _Shadow_Space_Height * 2.0f;
self.frame = viewFrameInMainView;
// set write frames for subviews
_imageViewCell.frame = CGRectMake(_Shadow_Space_Width, _Shadow_Space_Height, _imageViewCell.frame.size.width, _imageViewCell.frame.size.height);
self.clipsToBounds = YES;
[mainView addSubview:self];
}
-(void)bringViewOverSelectedCell:(UIView *)mainView{
if (self.cell == nil)
{
return;
}
// calculate position and frame
CGRect viewFrameInMainView = [mainView convertRect:self.cell.frame fromView:self.cell.superview];
viewFrameInMainView.origin.x -= _Shadow_Space_Width;
viewFrameInMainView.origin.y -= _Shadow_Space_Height;
viewFrameInMainView.size.width += _Shadow_Space_Width * 2.0f;
viewFrameInMainView.size.height += _Shadow_Space_Height * 2.0f;
self.frame = viewFrameInMainView;
}
@end
ViewController.h :
@interface MovingCellViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
MovingCell *_cellMoveAnimationView;
CGPoint _cellMoveLastTouchPoint;
NSIndexPath *_startIndexPath;
NSInteger _startTable;
id _selectedValue;
NSTimer *_autoScrollTimer;
}
@property(weak,nonatomic)IBOutlet UITableView *openTableView;
@property(weak,nonatomic)IBOutlet UITableView *inWorkTableView;
@property(weak,nonatomic)IBOutlet UITableView *inTestTableView;
@property(weak,nonatomic)IBOutlet UITableView *doneTableView;
@property(strong,nonatomic)NSMutableArray *dataOpenTable;
@property(strong,nonatomic)NSMutableArray *dataInWorkTable;
@property(strong,nonatomic)NSMutableArray *dataInTestTable;
@property(strong,nonatomic)NSMutableArray *dataDoneTable;
-(void)tableViewCellLongPress:(UILongPressGestureRecognizer *)gestureRecognizer;
-(void)dispatchLongPressOnCellBegan:(UILongPressGestureRecognizer *)gestureRecognizer;
-(void)dispatchLongPressOnCellChanged:(UILongPressGestureRecognizer *)gestureRecognizer;
-(void)dispatchLongPressOnCellEnded:(UILongPressGestureRecognizer *)gestureRecognizer;
-(void)dispatchLongPressOnCellCancelled:(UILongPressGestureRecognizer *)gestureRecognizer;
-(void)animatedCellAfterTouchEnd;
-(void)initArraysWithData;
ViewController.m:
#import "MovingCellViewController.h"
#import "HomeViewController.h"
@interface MovingCellViewController ()
@end
@implementation MovingCellViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataOpenTable = [NSMutableArray array];
self.dataInWorkTable = [NSMutableArray array];
self.dataInTestTable = [NSMutableArray array];
self.dataDoneTable = [NSMutableArray array];
[self initArraysWithData];
// Do any additional setup after loading the view.
}
-(void)initArraysWithData{
for (int i = 0; i < 5; ++i) {
[self.dataOpenTable addObject:[NSString stringWithFormat:@"US #%i",i]];
}
for (int i = 5; i < 10; ++i) {
[self.dataInWorkTable addObject:[NSString stringWithFormat:@"US #%i",i]];
}
for (int i = 10; i < 15; ++i) {
[self.dataInTestTable addObject:[NSString stringWithFormat:@"US #%i",i]];
}
for (int i = 15; i < 20; ++i) {
[self.dataDoneTable addObject:[NSString stringWithFormat:@"US #%i",i]];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.openTableView == tableView) {
return self.dataOpenTable.count;
}
if (self.inWorkTableView == tableView) {
return self.dataInWorkTable.count;
}
if (self.inTestTableView == tableView) {
return self.dataInTestTable.count;
}
if (self.doneTableView == tableView) {
return self.dataDoneTable.count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (self.openTableView == tableView) {
cell.tag = 1;
[cell.textLabel setText:[self.dataOpenTable objectAtIndex:indexPath.row]];
}
if (self.inWorkTableView == tableView) {
cell.tag = 2;
[cell.textLabel setText:[self.dataInWorkTable objectAtIndex:indexPath.row]];
}
if (self.inTestTableView == tableView) {
cell.tag = 3;
[cell.textLabel setText:[self.dataInTestTable objectAtIndex:indexPath.row]];
}
if (self.doneTableView == tableView) {
cell.tag = 4;
[cell.textLabel setText:[self.dataDoneTable objectAtIndex:indexPath.row]];
}
if ([cell.gestureRecognizers count] == 0)
{
{
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewCellLongPress:)];
[gestureRecognizer setMinimumPressDuration:0.2];
[cell addGestureRecognizer:gestureRecognizer];
}
}
return cell;
}
- (void)tableViewCellLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
//resign first responder
[self.view endEditing:YES];
switch ([gestureRecognizer state])
{
case UIGestureRecognizerStateBegan:
{
[self dispatchLongPressOnCellBegan:gestureRecognizer];
}
break;
case UIGestureRecognizerStateChanged:
{
[self dispatchLongPressOnCellChanged:gestureRecognizer];
}
break;
case UIGestureRecognizerStateEnded:
{
[self dispatchLongPressOnCellEnded:gestureRecognizer];
}
break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed:
{
[self dispatchLongPressOnCellCancelled:gestureRecognizer];
}
break;
default:
{
NSAssert(YES, @"Error: Other state is detected on the gesture recognizer of the cell: %i", [gestureRecognizer state]);
}
break;
}
}
- (void)dispatchLongPressOnCellBegan:(UILongPressGestureRecognizer *)gestureRecognizer
{
UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
// init if needed
if (_cellMoveAnimationView == nil)
{
_cellMoveAnimationView = [[MovingCell alloc] initWithFrame:CGRectMake(0.0f , 0.0f, 10.0f, 10.0f)];
}
if (cell.tag == 1)
{
_startIndexPath = [[self.openTableView indexPathForCell:cell] copy];
_startTable = 1;
_cellMoveAnimationView.tableView = self.openTableView;
_selectedValue = [self.dataOpenTable objectAtIndex:_startIndexPath.row];
}
if (cell.tag == 2) {
_startIndexPath = [[self.inWorkTableView indexPathForCell:cell] copy];
_cellMoveAnimationView.tableView = self.inWorkTableView;
_startTable = 2;
_selectedValue = [self.dataInWorkTable objectAtIndex:_startIndexPath.row];
}
if (cell.tag == 3) {
_startIndexPath = [[self.inTestTableView indexPathForCell:cell] copy];
_cellMoveAnimationView.tableView = self.inTestTableView;
_startTable = 3;
_selectedValue = [self.dataInTestTable objectAtIndex:_startIndexPath.row];
}
if (cell.tag == 4) {
_startIndexPath = [[self.doneTableView indexPathForCell:cell] copy];
_cellMoveAnimationView.tableView = self.doneTableView;
_startTable = 4;
_selectedValue = [self.dataDoneTable objectAtIndex:_startIndexPath.row];
}
// save start index path of this cell
_cellMoveAnimationView.hidden = NO;
_cellMoveAnimationView.alpha = 0.8f;
[_cellMoveAnimationView displayCell:cell inView:self.view];
[_cellMoveAnimationView addGestureRecognizer:gestureRecognizer];
_cellMoveLastTouchPoint = [gestureRecognizer locationInView:self.view];
cell.hidden = YES;
NSLog(@"tag of Cell : %i",cell.tag);
NSLog(@"StartIndex : %i",_startIndexPath.row);
}
- (void)dispatchLongPressOnCellChanged:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint touchPoint = [gestureRecognizer locationInView:self.view];
CGRect newFrame = _cellMoveAnimationView.frame;
newFrame.origin.y += touchPoint.y - _cellMoveLastTouchPoint.y;
newFrame.origin.x += touchPoint.x - _cellMoveLastTouchPoint.x;
_cellMoveLastTouchPoint = touchPoint;
_cellMoveAnimationView.frame = newFrame;
if (_cellMoveAnimationView.tableView != nil && CGRectContainsPoint(_cellMoveAnimationView.tableView.frame, touchPoint))
{
CGPoint movingCellCenterPointInTableView = [_cellMoveAnimationView.tableView convertPoint:_cellMoveAnimationView.center fromView:_cellMoveAnimationView.superview];
NSIndexPath *indexPath = [_cellMoveAnimationView.tableView indexPathForRowAtPoint:movingCellCenterPointInTableView];
// only switch the cell if auto scrolling is disabled, this fix the movement bug
[_cellMoveAnimationView.tableView moveRowAtIndexPath:[_cellMoveAnimationView.tableView indexPathForCell:_cellMoveAnimationView.cell] toIndexPath:indexPath];
}
// in table view 1?
else if (CGRectContainsPoint(self.openTableView.frame, touchPoint) && _cellMoveAnimationView.tableView == nil)
{
CGPoint movingCellCenterPointInTableView = [self.openTableView convertPoint:_cellMoveAnimationView.center fromView:_cellMoveAnimationView.superview];
NSIndexPath *indexPath = [self.openTableView indexPathForRowAtPoint:movingCellCenterPointInTableView];
if (indexPath)
{
_cellMoveAnimationView.tableView = self.openTableView;
[self.dataOpenTable insertObject:_selectedValue atIndex:indexPath.row];
[_cellMoveAnimationView.tableView beginUpdates];
[_cellMoveAnimationView.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:0];
[_cellMoveAnimationView.tableView endUpdates];
[_cellMoveAnimationView.tableView cellForRowAtIndexPath:indexPath].hidden = YES;
_cellMoveAnimationView.cell = [_cellMoveAnimationView.tableView cellForRowAtIndexPath:indexPath];
}
}
// in table view 2?
else if (CGRectContainsPoint(self.inWorkTableView.frame, touchPoint) && _cellMoveAnimationView.tableView == nil)
{
CGPoint movingCellCenterPointInTableView = [self.inWorkTableView convertPoint:_cellMoveAnimationView.center fromView:_cellMoveAnimationView.superview];
NSIndexPath *indexPath = [self.inWorkTableView indexPathForRowAtPoint:movingCellCenterPointInTableView];
if (indexPath)
{
_cellMoveAnimationView.tableView = self.inWorkTableView;
[self.dataInWorkTable insertObject:_selectedValue atIndex:indexPath.row];
[_cellMoveAnimationView.tableView beginUpdates];
[_cellMoveAnimationView.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:0];
[_cellMoveAnimationView.tableView endUpdates];
[_cellMoveAnimationView.tableView cellForRowAtIndexPath:indexPath].hidden = YES;
_cellMoveAnimationView.cell = [_cellMoveAnimationView.tableView cellForRowAtIndexPath:indexPath];
}
}
//in table view 3 ?
else if (CGRectContainsPoint(self.inTestTableView.frame, touchPoint) && _cellMoveAnimationView.tableView == nil)
{
CGPoint movingCellCenterPointInTableView = [self.inTestTableView convertPoint:_cellMoveAnimationView.center fromView:_cellMoveAnimationView.superview];
NSIndexPath *indexPath = [self.inTestTableView indexPathForRowAtPoint:movingCellCenterPointInTableView];
if (indexPath)
{
_cellMoveAnimationView.tableView = self.inTestTableView;
[self.dataInTestTable insertObject:_selectedValue atIndex:indexPath.row];
[_cellMoveAnimationView.tableView beginUpdates];
[_cellMoveAnimationView.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:0];
[_cellMoveAnimationView.tableView endUpdates];
[_cellMoveAnimationView.tableView cellForRowAtIndexPath:indexPath].hidden = YES;
_cellMoveAnimationView.cell = [_cellMoveAnimationView.tableView cellForRowAtIndexPath:indexPath];
}
}
//in table view 3 ?
else if (CGRectContainsPoint(self.doneTableView.frame, touchPoint) && _cellMoveAnimationView.tableView == nil)
{
CGPoint movingCellCenterPointInTableView = [self.doneTableView convertPoint:_cellMoveAnimationView.center fromView:_cellMoveAnimationView.superview];
NSIndexPath *indexPath = [self.doneTableView indexPathForRowAtPoint:movingCellCenterPointInTableView];
if (indexPath)
{
_cellMoveAnimationView.tableView = self.doneTableView;
[self.dataDoneTable insertObject:_selectedValue atIndex:indexPath.row];
[_cellMoveAnimationView.tableView beginUpdates];
[_cellMoveAnimationView.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:0];
[_cellMoveAnimationView.tableView endUpdates];
[_cellMoveAnimationView.tableView cellForRowAtIndexPath:indexPath].hidden = YES;
_cellMoveAnimationView.cell = [_cellMoveAnimationView.tableView cellForRowAtIndexPath:indexPath];
}
}
else if(_cellMoveAnimationView.tableView)
{
NSIndexPath *indexPath = [_cellMoveAnimationView.tableView indexPathForCell:_cellMoveAnimationView.cell];
if (indexPath)
{
if (_cellMoveAnimationView.tableView == self.openTableView)
{
[self.dataOpenTable removeObject:_selectedValue];
}
if (_cellMoveAnimationView.tableView == self.inWorkTableView)
{
[self.dataInWorkTable removeObject:_selectedValue];
}
if (_cellMoveAnimationView.tableView == self.inTestTableView)
{
[self.dataInTestTable removeObject:_selectedValue];
}
if (_cellMoveAnimationView.tableView == self.doneTableView)
{
[self.dataDoneTable removeObject:_selectedValue];
}
[_cellMoveAnimationView.tableView beginUpdates];
[_cellMoveAnimationView.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[_cellMoveAnimationView.tableView endUpdates];
_cellMoveAnimationView.tableView = nil;
}
}
}
- (void)dispatchLongPressOnCellEnded:(UILongPressGestureRecognizer *)gestureRecognizer
{
// if cell drop without seleceted table view
if (_cellMoveAnimationView.tableView == nil)
{
if (_startTable == 1)
{
_cellMoveAnimationView.tableView = self.openTableView;
[self.dataOpenTable insertObject:_selectedValue atIndex:_startIndexPath.row];
}
if (_startTable == 2)
{
_cellMoveAnimationView.tableView = self.inWorkTableView;
[self.dataInWorkTable insertObject:_selectedValue atIndex:_startIndexPath.row];
}
if (_startTable == 3)
{
_cellMoveAnimationView.tableView = self.inTestTableView;
[self.dataInTestTable insertObject:_selectedValue atIndex:_startIndexPath.row];
}
if (_startTable == 4)
{
_cellMoveAnimationView.tableView = self.doneTableView;
[self.dataDoneTable insertObject:_selectedValue atIndex:_startIndexPath.row];
}
[_cellMoveAnimationView.tableView beginUpdates];
[_cellMoveAnimationView.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:_startIndexPath] withRowAnimation:0];
[_cellMoveAnimationView.tableView endUpdates];
[_cellMoveAnimationView.tableView cellForRowAtIndexPath:_startIndexPath].hidden = YES;
_cellMoveAnimationView.cell = [_cellMoveAnimationView.tableView cellForRowAtIndexPath:_startIndexPath];
}
[_cellMoveAnimationView.cell addGestureRecognizer:gestureRecognizer];
// first animation will set the alpha back to 1.0f and bring the moving cell view over the cell in the tableview where it will drop
// second animation will hide the moving cell view with alpha 0.0f and will show the cell in the table view
// on completion the logic for the change will be set
[UIView animateWithDuration:0.3 animations:^{
//////// FIRST ANIMATION
_cellMoveAnimationView.alpha = 1.0f;
[_cellMoveAnimationView bringViewOverSelectedCell:self.view];
} completion:^(BOOL finished) {
/////// FIRST ANIMATION COMPLETION
[UIView animateWithDuration:0.2 animations:^{
///////// SECOND ANIMATION
_cellMoveAnimationView.cell.hidden = NO;
_cellMoveAnimationView.alpha = 0.0f;
} completion:^(BOOL finished) {
///////// SECOND ANIMATION COMPLETION
[_cellMoveAnimationView setHidden:YES];
_cellMoveAnimationView.cell.hidden = NO;
// index from start has changed and will never us now
_startIndexPath= nil;
}];
}];
}
/**
Long Press on cell cancelled, this function will handle the action at this event
@param gestureRecognizer = sender
*/
- (void)dispatchLongPressOnCellCancelled:(UILongPressGestureRecognizer *)gestureRecognizer
{
[self dispatchLongPressOnCellEnded:gestureRecognizer];
}
-(void)animatedCellAfterTouchEnd{
}
-(IBAction)saveAndBack:(id)sender{
HomeViewController*mainView = [self.storyboard instantiateViewControllerWithIdentifier:@"Home"];
[self.navigationController pushViewController: mainView animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
所以最后我很高兴,经过 3 天尝试不同的想法,Kristof 帮助我找到了正确的方法,我可以做我的工作来定制这个基本的解决方案。希望我们可以帮助其他人。 :) 周末愉快 :)
最好的问候康斯坦丁
【问题讨论】:
标签: ios objective-c uitableview drag-and-drop