UITableViewController [__NSArrayM objectAtIndex:]:索引0超出空数组的边界'与PFTask thenCallBackOnMainThreadAsync:]

时间:2014-09-09 10:27:28

标签: ios objective-c iphone uitableview

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

然而,它给了我这个错误。

为了调试,我甚至创建了一个空的UITableViewController并将storyboard文件引用到此。但是,它给了我同样的错误。

我刚尝试用空的UIViewController连接它,它给了我同样的objectAtIndex错误。

所以我怀疑这是我为细胞编制索引的问题。

当我跑步时,会显示屏幕,但它会抛出错误并冻结。

newsList的声明是:

@property (strong, nonatomic)NSArray *newsList

这就是我对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;



}

1 个答案:

答案 0 :(得分:0)

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

self.newsList=[[NSMutableArray alloc]init];//At viewWIllAppear

如果没有分配self.newsList,你就无法在其中存储任何记录......

希望它能解决......