2014-02-09 34 views
0

所以我正在爲我的一個朋友的東西,我有一些問題。我爲每個武器類別提供了一系列文本和圖像。應用程序運行良好,但後來我添加了圖像,現在它只是崩潰。看看,如果你有什麼想法,請讓我知道。謝謝!靜態TableView崩潰時,我添加圖像

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 [email protected][@"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 [email protected][@"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 

繼承人的崩潰日誌:

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) 
+0

您用盡陣列(3與2)的界限。請檢查您的代碼。我的意思是你只有混合了一些東西。 –

回答

1

我認爲這個問題是缺乏支架正確界定圍繞你的if/else語句範圍。見cellForRowAtIndexPath:

int row = [indexPath row]; 

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

else ... 

這這行代碼運行的每個cellForRowAtIndexPath被調用的時候,不管是哪個部分你indexPath是:

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

添加花括號你的if/else條件和你」請確保您所參考的那一行位於正確的部分。

+0

是的,我的if語句缺少大括號。感謝幫助我的每一個人。 – Jackintosh7

0

此崩潰的原因:

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

是您正在試圖從一些陣列的第四個對象(位於索引3的對象),但這個數組只有三個對象。我建議使用斷點來找出發生的地方和原因。

我希望它有幫助。