【发布时间】:2013-12-11 03:42:08
【问题描述】:
所以这让我发疯了,每当我在 UITableView 中点击一个项目时它什么都不做,但是当我在大约 3-5 秒后按住 UITableViewCell 时,它决定继续前进并做我想做的事.. 任何想法为什么会发生这种情况?
这是我的代码
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
_cell = [_arrayItems objectAtIndex:indexPath.row];
_cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
_cell = (CustomWidget *)[tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (_cell == nil) {
_cell = [[CustomWidget alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier title:[_arrayItems objectAtIndex:indexPath.row] subTitle:@"Custom subtitle"];
}
_cell.textLabel.text = [_arrayItems objectAtIndex:indexPath.row];
_cell.textLabel.hidden = YES;
return _cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arrayItems.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
_passedInPageTitle = selectedCell.textLabel.text;
[self openDetailPage];
}
【问题讨论】:
-
也许您的 CustomWidget 有一个 UIButton 或类似的东西成为 firstResponder 并接收所有这些事件?另外,在 didSelectRowAtIndexPath 之后,我通常会调用 `[tableView deselectRowAtIndexPath:indexPath animated:YES];` 以确保它被取消选择。
-
如果你在 didSelectRowAtIndexPath 中设置了一个断点,它是在你点击一个项目的第二秒还是在 3-5 秒后被命中?不相关,你用 cellForRowAtIndexPath 的前两行完成了什么?
-
另外,直接调用 cellForRowAtIndexPath 也不是很干净。您应该使用 indexPath 从模型 (_arrayItems) 中检索您需要的信息。据你所知,调用它可能会从内存中创建一个全新的单元格。
-
您是否记得将您的 tableview 的委托设置为实现 tableView:didSelectRowAtIndexPath: 的类?这个类是否声明它实现了 UITableViewDelegate?
-
你启发我创建了一个 StackOverflow 问题,我可以从现在开始参考;):stackoverflow.com/questions/20510288/…
标签: ios objective-c uitableview