【问题标题】:Why is NSArray empty at the time when UITableView Cell is selected?为什么选择 UITableView Cell 时 NSArray 为空?
【发布时间】:2011-10-03 04:03:27
【问题描述】:

我有一些朋友名字的文件列表。我想了解 UITableViewNavigation 。我从文件加载列表,但是当我选择一个单元格时,此时的数据数组似乎是空的,我不知道为什么。
在这个方法我得到这个错误

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        FriendsNameViewController *newView =[[FriendsNameViewController alloc] initWithNibName:@"FriendsNameViewController" bundle:nil];

        [self.navigationController pushViewController:newView animated:YES];
        //Crashes here
        NSString *temp = [dataArray objectAtIndex:indexPath.row];
        NSLog(@"%@",temp);
        [[newView labelx] setText:[NSString stringWithFormat: @"%@" ,temp]];
    }


2011-10-03 14:43:08.411 navman2[69691:b303] * 终止应用程序由于 未捕获的异常 'NSRangeException',原因:'* -[NSMutableArray objectAtIndex:]: 空数组的索引 2 超出范围'


界面

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController
{
    NSArray *dataArray;
}

@property(retain,nonatomic) NSArray *dataArray;

@end

然后在这里实现

#import "TableViewController.h"
#import "FriendsNameViewController.h"

@implementation TableViewController
@synthesize dataArray;

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

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myfriendsList" ofType:@"txt"];
    NSStringEncoding encoding;
    NSError* error;
    NSString* fh = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:&error];
    dataArray = [NSArray alloc];
    dataArray = [fh componentsSeparatedByString:@"\n"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];

    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    FriendsNameViewController *newView =[[FriendsNameViewController alloc] initWithNibName:@"FriendsNameViewController" bundle:nil];

    [self.navigationController pushViewController:newView animated:YES];
    //Crashes here
    NSString *temp = [dataArray objectAtIndex:indexPath.row];
    NSLog(@"%@",temp);
    [[newView labelx] setText:[NSString stringWithFormat: @"%@" ,temp]];
}

@end

【问题讨论】:

  • viewDidunload 中的代码更改为 self.dataArray = [NSMutableArray alloc]; self.dataArray = [fh componentsSeparatedByString:@"\n"]; 修复了它,但我没有得到区别。我知道它与 Autorelease 有关,但 self 有什么区别。

标签: objective-c xcode4


【解决方案1】:

如果您在选择之前验证了数组是正确的,那么我会尝试的第一件事就是稍微重新排列您的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        FriendsNameViewController *newView =[[FriendsNameViewController alloc] initWithNibName:@"FriendsNameViewController" bundle:nil];       
        NSString *temp = [dataArray objectAtIndex:indexPath.row];
        NSLog(@"%@",temp);
        [[newView labelx] setText:[NSString stringWithFormat: @"%@" ,temp]];
        [self.navigationController pushViewController:newView animated:YES];
    }

【讨论】:

  • 可能是由于数组自动释放吗?UIViewTable 填充正常,但数组变为空。将尝试您的代码。您认为可能是我的数组初始化不正确您认为什么。
  • 是的,当您在 viewDidLoad 中分配数组时,您可能想要使用 self.dataArray。如果我没记错的话,这将调用该属性的保留。
猜你喜欢
  • 2015-10-23
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多