问题描述
我有包含许多单元格的UITableView。用户可以通过按下此单元格中的展开按钮来扩展单元格以查看此单元格中的更多内容(只有1个单元格可以在时间扩展):
I have UITableView that contains many cell. User can expand cell to see more content in this cell by push the expand button in this cell (only 1 cell can expand at time):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedRowIndex == indexPath.row) return 205;
else return 60;
}
在故事板中,我将UILongPressGesture拖到单元格按钮并将其命名为longPress(cell是自定义的,它有2个按钮,1个需要识别LongPressGesture,另一个需要扩展单元格高度):
In the storyboard, I drag UILongPressGesture into cell button and named it longPress (cell is custom, it has 2 buttons in it, 1 need to recognize LongPressGesture, the other expand cell height):
@property (retain, nonatomic) IBOutlet UILongPressGestureRecognizer *longPress;
并且在viewDidLoad中:
And in the viewDidLoad:
- (void)viewDidLoad
{
[longPress addTarget:self action:@selector(handleLongPress:)];
}
它完美无缺,但是当我使用下面的代码识别单元格索引路径时,它是一个单元格扩展时出错:
It's work perfectly, however when I use following code to recognize cell indexPath, it's wrong when one cell is expanded:
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
// Get index path
slidePickerPoint = [sender locationInView:self.tableView];
NSIndexPath *indexPath= [self.tableView indexPathForRowAtPoint:slidePickerPoint];
// It's wrong when 1 cell is expand and the cell's button I hold is below the expand button
}
当有不同的小区高度时,有谁能告诉我如何获得正确的indexPath?
提前感谢你
Can anyone please show me how to get correct indexPath when there're different cell height?
Thank in advance
推荐答案
一种方法是将UILongPressGestureRecognizer添加到每个UITableViewCell(都使用相同的选择器),然后当调用选择器时,你可以得到单元格通过sender.view。也许不是最有效的内存,但如果单个手势识别器在某些情况下不会返回正确的行,这种方式应该可行。
One way to do it would be to add a UILongPressGestureRecognizer to each UITableViewCell (that all use the same selector), then when the selector is called you can get the cell via sender.view. Perhaps not the most memory efficient, but if the single gesture recognizer won't return the right row in certain situations, this way should work.
这样的事情:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
[longPress setMinimumPressDuration:2.0];
[cell addGestureRecognizer:longPress];
[longPress release];
return cell;
}
然后
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
UITableViewCell *selectedCell = sender.view;
}
这篇关于iOS - indexPathForRowAtPoint不返回具有不同单元格高度的正确indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!