Iphone 如果节为空,是否有合适的方法从CGPoint确定表中的节号?
我正在使用拖放行为在tableView中添加新元素。当我使用Iphone 如果节为空,是否有合适的方法从CGPoint确定表中的节号?,iphone,objective-c,ios,xcode,ipad,Iphone,Objective C,Ios,Xcode,Ipad,我正在使用拖放行为在tableView中添加新元素。当我使用 [tableView indexPathForRowAtPoint:CGPointMake(point.x-tableView.frame.origin.x, point.y-tableView.frame.origin.y+tableView.contentOffset.y)].section // point is an CGPoint instance 一切都很好。但当我将点悬停在空节标题视图下时,我会得到上一节编号。
[tableView indexPathForRowAtPoint:CGPointMake(point.x-tableView.frame.origin.x, point.y-tableView.frame.origin.y+tableView.contentOffset.y)].section // point is an CGPoint instance
一切都很好。但当我将点悬停在空节标题视图下时,我会得到上一节编号。这种方法行不通。所以我试图从headerView中的CGRectContainsPoint捕获事件,但没有成功,因为我无法从当前视图中获取节号。我试着根据章节设置标签
if (CGRectContainsPoint(CGRectMake([tableView viewWithTag:i+1].frame.origin.x + tableView.frame.origin.x, [tableView viewWithTag:i+1].frame.origin.y, tableView.frame.size.width, tableView.frame.size.height), CGPointMake(point.x, point.y + tableView.contentOffset.y))) {
[tableView viewWithTag:i+1].backgroundColor = [UIColor redColor];
}
但这种方法具有疯狂行为,与实际位置无关。我觉得自己有点弱智,我会感谢你的帮助
顺便说一句:我知道,如果我在空白部分添加虚拟单元格,这个问题就会消失,但我不能这样做,因为相关的问题。对不起,英语不好 使用fallowing代码,它将为您提供索引XPath,就像您在选择tableviewcell时如何进行树划分一样
-(UITableViewCell *)cellForReuseIdentiFier:(NSString*)identifier andindex:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *circleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[circleBtn addTarget:self action:@selector(selectSongAction:) forControlEvents:UIControlEventTouchUpInside];
[circleBtn setTag:Selection_Tag];
[circleBtn setBackgroundImage:[UIImage imageNamed:@"Demo03.png"] forState:UIControlStateNormal];
[circleBtn setFrame:CGRectMake(6, 15, 16, 16)];
[cell.contentView addSubview:circleBtn];
return cell;
}
-(void)selectSongAction:(id)sender
{
CGPoint btnPostion = [sender convertPoint:CGPointZero toView:songTable];
NSIndexPath *indexPath = [songTable indexPathForRowAtPoint:btnPostion];
}
这可能是一个解决办法。这对我有用。您必须在每个rectForHeaderInSection:上迭代,以查看位置是否指向其中一个部分。如果其中一个包含CGPoint,则获取该CGPoint并将行移动到指向的部分
CGPoint location = [longPress locationInView:self.orderTableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
int i = 0;
while (i < self.datasource.count) {
CGRect rect = [self.tableView rectForHeaderInSection:i];
if (CGRectContainsPoint(rect, location)) {
indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:i];
break;
}
i++;
}
这不是对您问题的回答,但您可能会发现UIView的观点:。。。和convertRect:。。。方法对清理代码非常有用,而不是手动计算新的点和矩形。感谢您的建议,我很快编写了代码,还有很多东西需要重构。