Ios UITableViewController[uu NSArrayM objectAtIndex:]:索引0超出空数组的界限';使用PFTask,然后调用MainThreadAsync:]

Ios UITableViewController[uu NSArrayM objectAtIndex:]:索引0超出空数组的界限';使用PFTask,然后调用MainThreadAsync:],ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我有意创建一个空数组,以便在UITablewView上不显示任何内容 然而,它给了我这个错误 为了调试,我甚至创建了一个空的UITableViewController,并将情节提要文件引用到此。然而,它给了我同样的错误 我刚刚尝试将其连接到一个空的UIViewController,它给了我相同的objectAtIndex错误 所以我怀疑这是我为单元格编制索引的问题所在 当我运行时,屏幕会显示,但它抛出错误并冻结 新闻列表的声明如下: @property(强,非原子)NSArray*新闻列表 这是

我有意创建一个空数组,以便在UITablewView上不显示任何内容

然而,它给了我这个错误

为了调试,我甚至创建了一个空的UITableViewController,并将情节提要文件引用到此。然而,它给了我同样的错误

我刚刚尝试将其连接到一个空的UIViewController,它给了我相同的objectAtIndex错误

所以我怀疑这是我为单元格编制索引的问题所在

当我运行时,屏幕会显示,但它抛出错误并冻结

新闻列表的声明如下:

@property(强,非原子)NSArray*新闻列表

这是我为UITableViewController准备的

- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];

    self.currentUser = appDelegate.currentUser;

    NSString *addNewsFeed = @"_NewsFeed";


    if (self.currentUser)
    {


        if (appDelegate.selectedGroup == nil)
        {
            self.newsList = nil;


        }
        else
        {
            NSLog(@"SELECTED GROUP EXIST");
            NSString *currentNewsFeed = [appDelegate.selectedGroup[@"name"] stringByAppendingString:addNewsFeed];

            PFQuery *query = [PFQuery queryWithClassName:currentNewsFeed];
            [query orderByDescending:@"createdAt"];

            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

                if (error)
                {
                    NSLog(@"Error: %@, %@", error, [error userInfo]);
                }
                else
                {
                    self.newsList = objects;
                    [self.tableView reloadData];
                }
            }];
        }



    }
    else
    {
        NSLog(@"%@", appDelegate.currentUser);
        [self performSegueWithIdentifier:@"loginView" sender:self];

    }

    NSLog(@"ZXCVZCVZ: %@", self.newsList);



}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (appDelegate.selectedGroup == nil)
    {
        NSLog(@"NO CELL HERE");

        return 0;
    }

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (appDelegate.selectedGroup == nil)
    {
        NSLog(@"NO CELL");
        return 0;
    }

    return [self.newsList count];
}


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

    NSLog(@"NO LIST FOUND");
    static NSString *CellIdentifier = @"News";
    NSLog(@"DSFSDFSDFSFS");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    PFObject *item = [self.newsList objectAtIndex:indexPath.row];
    cell.textLabel.text = item[@"title"];
    cell.detailTextLabel.text = item[@"news"];

    return cell;



}

您需要为数组分配内存,如下所示

self.newsList=[[NSMutableArray alloc]init];//At viewWIllAppear
没有alloc self.newsList,您无法在其中存储任何记录


希望它能修复…

您能提供您得到的确切错误吗?请发布新闻列表属性声明。@danh我刚刚发布了!哪里是异常堆栈跟踪,您应该提供任何与异常相关的问题??OP用查询结果替换新闻列表,因此此初始化将无效。在查询结果之前,nil数组将有一个count==0,这将阻止调用cellForRowAtIndexPath。如果他没有分配数组,他将不会得到错误。