2013-08-29 74 views
1

在remove方法中,我的程序似乎一直很好,直到它退出並拋出一個超出界限的異常。它正確地從我的表視圖(tableView2)連接到的可變數組中刪除項目。我的表正確連接到委託和數據源。在reloadData行之後拋出異常。我有這種方法工作在一個不同的版本,我失去了一個操作系統重新安裝,但我認爲這是我以前的工作。任何建議將不勝感激!_tableView reloadData崩潰我的應用程序

錯誤:

2013-08-29 00:48:58.451 Event Sign-In[770:11303] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 
*** First throw call stack: 
(0x1c94012 0x10d1e7e 0x1c360b4 0x27a4 0xd08fb 0xd09cf 0xb91bb 0xc9b4b 0x662dd 0x10e56b0 0x2290fc0 0x228533c 0x2285150 0x22030bc 0x2204227 0x22048e2 0x1c5cafe 0x1c5ca3d 0x1c3a7c2 0x1c39f44 0x1c39e1b 0x1bee7e3 0x1bee668 0x15ffc 0x1cdd 0x1c05 0x1) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

.h: 

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{ 
    UITableView *tableView; 
} 

@property (weak, nonatomic) IBOutlet UITextField *nameField; 
@property (nonatomic, retain) NSMutableArray *names; 
@property (nonatomic, retain) NSMutableArray *existingNames; 
@property (nonatomic, retain) NSMutableArray *companies; 
@property (nonatomic, retain) NSMutableArray *namesAndCompanies; 

- (IBAction)add:(id)sender; 
- (IBAction)addExisting:(id)sender; 
- (IBAction)Edit:(id)sender; 
- (IBAction)Remove:(id)sender; 
- (IBAction)submit:(id)sender; 
- (IBAction)removeAll:(id)sender; 

@property (strong, nonatomic) IBOutlet UITableView *tableView1; 
@property (weak, nonatomic) IBOutlet UITableView *tableView2; 
@property (strong, nonatomic) IBOutlet UITableView *companiesTable; 

@end 

.M:

#import "ViewController.h" 
#import <MessageUI/MessageUI.h> 

@interface ViewController() <MFMailComposeViewControllerDelegate> 

@end 

@implementation ViewController 

@synthesize nameField; 
@synthesize names; 
@synthesize companies; 
@synthesize existingNames; 
@synthesize namesAndCompanies; 
@synthesize tableView1 = _tableView1; 
@synthesize tableView2 = _tableView2; 
@synthesize companiesTable = _companiesTable; 

int rowNumber1; 
int rowNumber2; 
int companyRowNumber; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.names = [[NSMutableArray alloc] init]; 
    self.existingNames = [[NSMutableArray alloc] init]; 
    self.namesAndCompanies = [[NSMutableArray alloc] init]; 
    self.companies = [NSArray arrayWithObjects:@"Morgridge", @"WID", @"WARF", @"Other", nil]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.tableView1){ 
     return [existingNames count]; 
    } 
    else if (tableView == self.tableView2){ 
     return [names count]; 
    } 
    else if (tableView == self.companiesTable){ 
     return [companies count]; 
    } 
    return 0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 
    UITableViewCell *cell; 

    if (tableView == self.tableView1){ 
     cell = [_tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    } 
    else if (tableView == self.tableView2){ 
     cell = [_tableView2 dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    } 
    else if (tableView == self.companiesTable){ 
     cell = [_companiesTable dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    } 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    if (tableView == self.tableView1){ 
     cell.textLabel.text = [existingNames objectAtIndex:indexPath.row]; 
    } 
    else if (tableView == self.tableView2){ 
     cell.textLabel.text = [namesAndCompanies objectAtIndex:indexPath.row]; 
    } 
    else if (tableView == self.companiesTable){ 
     cell.textLabel.text = [companies objectAtIndex:indexPath.row]; 
    } 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (tableView == self.tableView1){ 
     rowNumber1 = indexPath.row; 
    } 
    else if (tableView == self.tableView2){ 
     rowNumber2 = indexPath.row; 
    } 
    else if (tableView == self.companiesTable){ 
     companyRowNumber = indexPath.row; 
    } 


} 

- (IBAction)add:(id)sender { 
    if ([nameField.text isEqualToString:@""]){ 
     return; 
    } 

    BOOL exists = [names containsObject:nameField.text]; 

    if(exists == FALSE){ 
     NSMutableString *nameAndCompany = nameField.text; 
     [nameAndCompany appendString:@", "]; 
     [nameAndCompany appendString:[companies objectAtIndex:companyRowNumber]]; 

     [names addObject:nameField.text]; 
     [namesAndCompanies addObject:nameAndCompany]; 
     [existingNames addObject:nameAndCompany]; 
    } 
    else{ 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:@"A user with that name already exists." 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 

    [names sortUsingSelector:@selector(compare:)]; 
    [namesAndCompanies sortUsingSelector:@selector(compare:)]; 
    [existingNames sortUsingSelector:@selector(compare:)]; 

    [_companiesTable deselectRowAtIndexPath:[_companiesTable indexPathForSelectedRow] animated:YES]; 

    [self.view endEditing:YES]; 

    [_tableView1 reloadData]; 
    [_tableView2 reloadData]; 

    [email protected]""; 
} 

- (IBAction)addExisting:(id)sender { 
    if ([existingNames containsObject:[namesAndCompanies objectAtIndex:rowNumber1]]){ 
     return; 
    } 

    [existingNames addObject:[namesAndCompanies objectAtIndex:rowNumber1]]; 
    [existingNames sortUsingSelector:@selector(compare:)]; 

    [_tableView1 deselectRowAtIndexPath:[_tableView1 indexPathForSelectedRow] animated:YES]; 

    [_tableView2 reloadData]; 
} 

- (IBAction)Edit:(id)sender { 
    int toDelete = rowNumber1; 
    nameField.text = [names objectAtIndex:rowNumber1]; 
    if ([namesAndCompanies containsObject:[existingNames objectAtIndex:rowNumber1]]){ 
     [namesAndCompanies removeObjectIdenticalTo:[existingNames objectAtIndex:rowNumber1]]; 
    } 
    [existingNames removeObjectAtIndex:toDelete]; 
    [names removeObjectAtIndex:toDelete]; 

    [_tableView1 deselectRowAtIndexPath:[_tableView1 indexPathForSelectedRow] animated:YES]; 

    [_tableView1 reloadData]; 
    [_tableView2 reloadData]; 
} 

- (IBAction)Remove:(id)sender { 
    [namesAndCompanies removeObjectAtIndex:rowNumber2]; 


    [_tableView2 reloadData]; 
} 


- (IBAction)removeAll:(id)sender { 

} 
@end 
+0

我只知道你的數組是空的 –

+0

是的...和reloadData應該看看該數組並相應地更新tableView。 –

+1

在 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分檢查哪一個數組對應於哪個表視圖導致問題。 –

回答

1

umer sufyan在評論中指出我正確的方向。我正在計數numberOfRowsInSection方法中的錯誤數組。

2
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    else if (tableView == self.tableView2){ 
     return [names count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    else if (tableView == self.tableView2){ 
     cell.textLabel.text = [namesAndCompanies objectAtIndex:indexPath.row]; 
    } 
} 

您使用的名稱數組來計算行數。和namesAndCompanies數組來選擇行的對象。如果名稱Array返回1,並且namesAndCompanies Array爲空,則應用程序將崩潰。

+0

已在評論中回答。 –