我知道這將成爲那些令人頭疼的時刻之一。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