[英]Problems when dismissing a modal UIViewController with a UITableView in it
所以我有一個UIViewController
,以模態形式呈現在當前視圖上。 該視圖控制器僅由一個UITableView
(用於選擇項目)和一個頂部的導航欄組成,該導航欄在您不想選擇任何內容的情況下具有“取消”按鈕。
幾乎在任何情況下,整個過程都可以正常工作。 選擇一個項目有效,按“取消”按鈕關閉視圖,一切都很好。 但是,有一種情況導致應用程序崩潰:當您在表格視圖中向左滑動某個項目以顯示“刪除”按鈕,然后按頂部的“取消”按鈕以關閉該視圖時,應用程序崩潰了,但沒有崩潰在控制台輸出中說出崩潰的原因。 這是我模態呈現的視圖控制器的代碼:
。H:
#import <UIKit/UIKit.h>
#import "common.h"
@interface LoadViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
- (IBAction)lCancelButton:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *lTableView;
@end
.m:
#import "LoadViewController.h"
@interface LoadViewController () {
NSMutableArray* sampleCounts;
NSArray* tableData;
}
@end
@implementation LoadViewController
@synthesize lTableView = _lTableView; // This is the table view itself
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Deleting an item when the delete button is pressed
[self.lTableView beginUpdates];
// Deleting it from the table view first
[self.lTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// ... then from the arrays as well
[sampleCounts removeObjectAtIndex:indexPath.item];
// (making a mutable copy here so I can delete stuff from it)
NSMutableArray* tmp = [tableData mutableCopy];
[tmp removeObjectAtIndex:indexPath.item];
tableData = tmp;
[self.lTableView endUpdates];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.lTableView.allowsMultipleSelectionDuringEditing = NO;
sampleCounts = [NSMutableArray array];
tableData = [NSMutableArray array];
// I'm filling up both arrays with the appropriate data here...
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)lCancelButton:(id)sender {
// Dismiss the view controller when Cancel is pressed
[self dismissViewControllerAnimated:YES completion:nil];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.tableData.count;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// If an item gets selected, store the name of it (I use a class called 'common' for storing data like this), then dismiss the view
[common setItemName:[tableData objectAtIndex:indexPath.item]];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* ID = @"ID";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.textLabel.text = [self.tableData objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [sampleCounts objectAtIndex:[indexPath row]];
return cell;
}
@end
如您所見,這非常簡單。 我將項目名稱和它們的描述放在兩個單獨的數組中(兩個數組都包含NSString*
),然后用該信息填充表格視圖,僅此而已。 其余的很簡單。
那么,有誰知道為什么我在某個項目上滑動以顯示“刪除”按鈕,然后通過按“取消”來關閉視圖控制器時,應用程序崩潰? 在任何其他情況下,一切都可以正常工作。 僅當我看到刪除按鈕時按“取消”時,它才會崩潰。
請嘗試以下操作:
- (void)viewWillDisappear:(BOOL)animated
{
self.lTableView.editing = NO;
}
我能想到的是您的模式。 可能是您處於編輯模式。 因此,在關閉之前,請嘗試回到正常模式。
[self.tableView setEditing:No];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.