2013-08-29 4 views
1

В методе удаления моя программа работает нормально, пока она не выйдет и исключает исключение из-за пределов. Он правильно удаляет элемент из изменяемого массива, к которому подключен мой табличный вид (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]; 
    } 
} 

Вы можете использовать имена массивов для подсчета количества строк. И именаAndCompanies Array для выбора объекта для строки. Если имена Array возвращают 1 и namesAndCompanies Array пуст, приложение выйдет из строя.

+0

Уже ответил на комментарии. –

Смежные вопросы