1

在 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

米:

#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
4

2 回答 2

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 数组来选择行的对象。如果 names Array 返回 1 并且 namesAndCompanies Array 为空,应用程序将崩溃。

于 2013-08-29T06:21:30.913 回答
1

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

于 2013-08-29T06:25:39.180 回答