質問

So I'm working on something for a friend of mine, and I'm having some issues. I have an array of text and images for each weapon category.The application was working perfectly but then I added images and now It just crashes. Take a look and if you have any ideas on whats happing please let me know. Thanks!

tableview.m

#import "TableViewController.h"
#import "TableCell.h"


@interface TableViewController ()
@end
@implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];

    _assault = @[@"SC-2010",
                 @"SA-805",
                 @"AK-12",
                 @"FAD",
                 @"Remington R5",
                 @"MSBS",
                 @"Honey Badger",
                 @"ARX-150",];
    _smg= @[@"Bizon",
            @"CBJ-MS",
            @"Vector CRB",
            @"Vepr",
            @"K7",
            @"MTAR-X",];

    _lmg =@[@"Ameli",
            @"M27-IAR",
            @"LSAT",
            @"Chain SAW",];
    _marksman = @[@"MR-28",
                  @"MK14 EBR",
                  @"IA-2",
                  @"SVU",];
    _snipers = @[@"USR",
                 @"L115",
                 @"Lynx",
                 @"VKS",];
    _shotguns = @[@"Tac 12",
                  @"FP6",
                  @"MTS-225",
                  @"Bulldog",];
    _handguns = @[@"M9",
                  @"Magnum",
                  @"MP-443",
                  @"P226",
                  @"PDW",];
    _launchers = @[@"Kastet",
                   @"MK32",
                   @"Panzerfast",];

    /////IMAGES/////


    _assault1 = @[@"sc-2010.png",
                 @"sa-805.png",
                 @"ak12.png",
                 @"fad.png",
                 @"remmington.png",
                 @"msbs.png",
                 @"honey badger.png",
                 @"arx.png",];
    _smg1= @[@"Bizon.png",
            @"cbj",
            @"ector.png",
            @"vepr.png",
            @"k7.png",
            @"mtar-x.png",];

    _lmg1 =@[@"ameli.png",
            @"m27.png",
            @"lsat.png",
            @"chain saw.png",];
    _marksman1 = @[@"mr-28.png",
                  @"mk14",
                  @"ia2.png",
                  @"svu.png",];
    _snipers1 = @[@"usr.png",
                 @"l115.png",
                 @"lynx.png",
                 @"VKS.png",];
    _shotguns1 = @[@"tac 12.png",
                  @"fp6.png",
                  @"mts-225",
                  @"bulldog.png",];
    _handguns1 = @[@"m9.png",
                  @"magum.png",
                  @"mp.png",
                  @"p226.png",
                  @"pdw",];
    _launchers1 = @[@"kastet.png",
                   @"mk32.png",
                   @"panzerfast.png",];


}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 8;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0)
        return _assault.count;
    if (section==1)
        return _smg.count;
    if (section==2)
        return _lmg.count;
    if (section==3)
        return _marksman.count;
    if (section==4)
        return _snipers.count;
    if (section==5)
        return _shotguns.count;
    if (section==6)
        return _handguns.count;
    if (section==7)
        return _launchers.count;
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if(section == 0)
        return @"Assault Rifles";
    if(section==1)
        return @"SMG";
    if (section==2)
        return @"LMG";
    if (section==3)
        return @"Marksman";
    if (section==4)
        return @"Sniper";
    if (section==5)
        return @"Shotguns";
    if (section==6)
        return @"Handguns";
    if (section==7)
        return @"Launchers";
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"TableCell";
     TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    int row = [indexPath row];

    if (indexPath.section==0)
        cell.TitleLabel.text = _assault[row];
        cell.myImageView.image = [UIImage imageNamed:_assault1[row]];
    if (indexPath.section==1)
        cell.TitleLabel.text = _smg[row];
        cell.myImageView.image = [UIImage imageNamed:_smg1[row]];
    if (indexPath.section==2)
        cell.TitleLabel.text = _lmg[row];
           cell.myImageView.image = [UIImage imageNamed:_lmg1[row]];
    if (indexPath.section==3)
        cell.TitleLabel.text = _marksman[row];
       cell.myImageView.image = [UIImage imageNamed:_marksman1[row]];
    if (indexPath.section==4)
        cell.TitleLabel.text = _snipers[row];
       cell.myImageView.image = [UIImage imageNamed:_snipers1[row]];
    if (indexPath.section==5)
        cell.TitleLabel.text = _shotguns[row];
       cell.myImageView.image = [UIImage imageNamed:_shotguns1[row]];
    if (indexPath.section==6)
        cell.TitleLabel.text = _handguns[row];
        cell.myImageView.image = [UIImage imageNamed:_handguns1[row]];
    if (indexPath.section==7)
        cell.TitleLabel.text = _launchers[row];
        cell.myImageView.image = [UIImage imageNamed:_launchers1[row]];
    return cell;
}
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end

Heres the crash log:

2014-02-09 18:29:43.286 Test[4248:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101894795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001015f7991 objc_exception_throw + 43
    2   CoreFoundation                      0x000000010184d02f -[__NSArrayI objectAtIndex:] + 175
    3   Ghosts Guide & Utility1             0x0000000100003451 -[TableViewController tableView:cellForRowAtIndexPath:] + 3265
    4   UIKit                               0x0000000100325b8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
    5   UIKit                               0x000000010030d836 -[UITableView _updateVisibleCellsNow:] + 2297
    6   UIKit                               0x000000010031e381 -[UITableView layoutSubviews] + 207
    7   UIKit                               0x00000001002b5b27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
    8   QuartzCore                          0x0000000103dfba22 -[CALayer layoutSublayers] + 151
    9   QuartzCore                          0x0000000103df0589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
    10  QuartzCore                          0x0000000103df040a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    11  QuartzCore                          0x0000000103d65694 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
    12  QuartzCore                          0x0000000103d6670c _ZN2CA11Transaction6commitEv + 394
    13  QuartzCore                          0x0000000103d66d79 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    14  CoreFoundation                      0x000000010185fff7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    15  CoreFoundation                      0x000000010185ff67 __CFRunLoopDoObservers + 391
    16  CoreFoundation                      0x000000010183f6d2 __CFRunLoopRun + 946
    17  CoreFoundation                      0x000000010183ef33 CFRunLoopRunSpecific + 467
    18  GraphicsServices                    0x000000010399b3a0 GSEventRunModal + 161
    19  UIKit                               0x000000010025a043 UIApplicationMain + 1010
    20  test                                0x0000000100001393 main + 115
    21  libdyld.dylib                       0x0000000101f235fd start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
役に立ちましたか?

解決

I think the problem is the lack of brackets properly defining scope around your if/else statements. See this code in cellForRowAtIndexPath:

int row = [indexPath row];

if (indexPath.section==0)
    cell.TitleLabel.text = _assault[row];
    cell.myImageView.image = [UIImage imageNamed:_assault1[row]];

else ... 

This line is run every time cellForRowAtIndexPath is called, regardless of which section your indexPath is on:

cell.myImageView.image = [UIImage imageNamed:_assault1[row]];

Add curly brackets to your if/else conditions and you'll make sure that the row you're referencing there is in the proper section.

他のヒント

The reason of this crash:

[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]

Is that you are trying to get the the fourth object (object at index 3) from some array, but the this array only has three objects. I suggest to use breakpoints to discover where and why this is happening.

I hope it helps.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top