添加图像时静态TableView崩溃

时间:2014-02-09 23:37:15

标签: ios objective-c uitableview custom-cell

所以我正在为我的一个朋友做点什么,而且我遇到了一些问题。我为每个武器类别提供了一系列文本和图像。应用程序运行良好,但后来我添加了图像,现在它只是崩溃了。看一看,如果您有什么想法,请告诉我。谢谢!

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

继承崩溃日志:

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) 

2 个答案:

答案 0 :(得分: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条件中添加花括号,您将确保您所引用的行位于正确的部分。

答案 1 :(得分:0)

这次崩溃的原因:

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

您是否正在尝试从某个数组中获取第四个对象(索引3处的对象),但此数组只有三个对象。我建议使用断点来发现这种情况发生的地点和原因。

我希望它有所帮助。