在我的ipad应用程序中,我需要拖放 UIButton
来插入一个单元格 . 我设法通过touchupinside事件拖动一个按钮并插入一个单元格 . 它适用于以下代码 -
//button1 is initially placed in a view called scrollview
[button1 addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button1 removeTarget:self action:@selector(insertcell) forControlEvents:UIControlEventTouchUpInside];
CGPoint pointfordeterminingrow;
- (IBAction) imageMoved:(UIButton *) sender withEvent:(UIEvent *) event {
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
pointfordeterminingrow=point; // point where the button is hovering now
if ([sender state] == UIGestureRecognizerStateBegan) {
[self.view addSubview:sender];
}
if ([sender state] == UIGestureRecognizerStateChanged) {
[scrollview addSubview:sender];
}
if ([sender state] == UIGestureRecognizerStateEnded) {
[scrollview addSubview:sender];
}
}
- (int)rowAtPoint:(CGPoint)point {
NSIndexPath* newIndexPath = [MainTableView indexPathForRowAtPoint:point];
return newIndexPath == nil ? [items count] : newIndexPath.row;
//items is a mutable array has the content of each cell
}
-(void)insertcell {
int row = [self rowAtPoint:pointfordeterminingrow];
NSIndexPath* newIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
[self insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:newIndexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}
这些代码在索引路径处插入一个单元格,在该索引路径上按下按钮//调用触摸方法 .
我的问题是:
我得到的“newIndexPath”是一个数字(tableview中的行),按钮被删除 . 这个“insertcell”方法在没有滚动tableview的情况下工作正常,
当滚动TableView时,“rowAtPoint”返回一个数字,该数字是indexpath的行,截至目前为止是可见的 .
换句话说,如果按钮被丢弃在索引路径58上方并且如果上面隐藏了50个单元格(通过滚动),我得到数字8,(不是58)
因此,当在索引路径58处按下按钮时,在INDEXPATH 8处插入单元 .
我需要建议在一个点的tableview中获取EXACT索引路径 . 如果“点”是自我视图中的CGPoint .
1 回答
试试这个: