_tableView reloadData崩溃我的应用程序

时间:2013-08-29 06:03:26

标签: ios objective-c uitableview ios6 tableview

在remove方法中,我的程序看起来很好,直到它退出并抛出一个超出范围的异常。它从我的表视图(tableView2)所连接的可变数组中正确地删除了该项。我的表正确连接到委托和数据源。在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];

    nameField.text=@"";
}

- (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

2 个答案:

答案 0 :(得分: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];
    }
}

使用名称Array来计算行数。和namesAndCompanies Array一起选择行的对象。如果名称Array返回1且namesAndCompanies Array为空,则应用程序将崩溃。

答案 1 :(得分:1)

umer sufyan在评论中指出了正确的方向。我在numberOfRowsInSection方法中计算了错误的数组。