我知道这将成为那些令人头疼的时刻之一。UITableView didSelectRowAtIndexPath有时只会被解雇
我有一个UITableView,我需要从中删除单元格。问题是,didSelectRowAtIndexPath仅在我第一次点击该行然后滑动以显示删除按钮时被调用。如果我在没有明确点击该行的情况下刷动该行,则会出现EXC_BAD_ACCESS错误。我把一个NSLog放入didSelectRowAtIndexPath中,当我点击单元格时可以看到indexPath数组,但是如果我只是轻轻滑动而没有点击就可以看到。
我已经完成了我的搜索分享,并且我99%确定我的委托已正确连接(通常是问题#1)并且没有键入didDeselectRowAtIndexPath(通常是问题#2)。我也有我的.h文件(问题#3)。
在此先感谢。
编辑:
我应该说:如果我刷卡行没有明确点击行,然后我得到的时候我点击删除按钮的EXC_BAD_ACCESS错误。如果点击单元格然后刷卡,我可以点击删除,并按预期方式删除。
编辑:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[bandList sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
NSDictionary *dict = [bandList objectAtIndex:indexPath.row];
cell.textLabel.text = [dict objectForKey:@"name"];
return cell;
}
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
NSLog(@"IndexPath: %@", [indexPath description]);
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSDictionary *selectedBand = [bandList objectAtIndex:indexPath.row];
NSString *selectedBandId = [selectedBand objectForKey:@"id"];
[self deleteMyBand:selectedBandId];
bandList = [self getSavedBands];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
请发布您的代码didSelectRowAtIndexPath,删除委托方法和cellForRowAtIndexPath。 –
感谢您的帮助,@ XcodeDev。我更新了原始帖子。 – voodoobilly
每当tableView要求一个新的单元时,你正在对你的'bandList'进行排序。 'cellForRowAtIndexPath'不是准备数据的最佳位置。另外,如果'bandList'随时间变化,你的tableView将会变得不一致。 – kelin