在我的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时#34; rowAtPoint"返回一个数字,它是indexpath的行,截至目前为止是可见的。
换句话说,如果按钮被丢弃在索引路径58上方并且如果上面隐藏了50个单元格(通过滚动),我得到数字8,(不是58)
因此,当在索引路径58处删除按钮时,将在INDEXPATH 8中插入一个单元格。
我需要建议在一个表格中获取EXACT索引路径。提供了"点"是自我视图中的CGPoint。
答案 0 :(得分:1)
试试这个:
int row = [self rowAtPoint:pointfordeterminingrow];
[myTableView visibleCells]; // call this first to avoid possible iOS bug in next statement
NSArray *visIndexPaths = [myTableView indexPathsForVisibleRows];
NSIndexPath* newIndexPath = [visIndexPaths objectAtIndex:row];