2013-04-10 154 views
0
cell = [[[NSBundle mainBundle] loadNibNamed:@"Empty" owner:self options:nil] objectAtIndex:0]; 
    cell = [[[NSBundle mainBundle] loadNibNamed:@"Cell1" owner:self options:nil] objectAtIndex:1]; 

我得到一個超出邊界誤差的索引。我試圖改變數字,但沒有運氣! 任何幫助將不勝感激。我一直在使用谷歌搜索,所有在網上找到的解決方案都沒有幫助。謝謝。'*** - [__ NSArrayM objectAtIndex:]:index 1 beyond bounds [0..0]'

.M

#import "SettingsViewController.h" 
@interface SettingsViewController() 

@end 

@implementation SettingsViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

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

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    return 2; 
} 
// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView 
          dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     cell = [[[NSBundle mainBundle] loadNibNamed:@"Empty" owner:self options:nil] objectAtIndex:0]; 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"Cell1" owner:self options:nil] objectAtIndex:1]; 

    } 


    return cell; 
} 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSString *sectionName; 
    switch (section) 
    { 
     case 0: 
      sectionName = NSLocalizedString(@"General", @"General"); 
      break; 
     case 1: 
      sectionName = NSLocalizedString(@"myOtherSectionName", @"myOtherSectionName"); 
      break; 
      // ... 
     default: 
      sectionName = @""; 
      break; 
    } 
    return sectionName; 
} 
#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 
@end 
+2

嘗試[here](http://stackoverflow.com/questions/15914626/ios-dev-nsarray-for-uitableview-labels-returning-index-1-out-of-bounds)。你和user2264008似乎在同一個班級。 – 2013-04-10 00:42:23

+3

(當然,數組索引越界是你的問題中最少的。) – 2013-04-10 00:44:32

回答

0

加載自定義筆尖我使用,

使NSObject的範疇類,並在其中添加該代碼。

+ (id)loadNibNamed:(NSString *)NibName { 
    NSObject *cl = nil; 
    if (NSClassFromString(NibName) != nil) { 
     NSArray *arr = [[NSBundle mainBundle] loadNibNamed:NibName owner:self options:nil]; 
     for (id cls in arr) { 
      if([cls isKindOfClass:NSClassFromString(NibName)]) 
      { 
       cl = cls; 
       break; 
      } 
     } 
    } 
    return cl; 
} 

,並要求它像,

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) { 
     cell = (UITableViewCell *)[[class class] loadNibNamed:NSStringFromClass(class)]; 
    } 

它會幫助你。

相關問題