UITableViewCell保持空白

时间:2015-05-08 07:39:39

标签: ios objective-c uitableview

我正在尝试使用从Parse中的表填充的NSMutableArray的对象填充UITableView。数组肯定是填充的(我用NSLog检查了它的内容),但表是空的。我尝试了很多不同的方法,包括:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    PFObject *postsObject = [postsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [postsObject objectForKey:@"message"];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;

    if (!cell) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    if (tableView == table) {
        cell.textLabel.text = [postsArray objectAtIndex:indexPath.row];
    }

    return cell;
}

和更简单的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"Cell"];

    cell.messageLabel.text = [NSString stringWithFormat:[postsArray objectAtIndex:[indexPath row]]];

    return cell;
}

有没有人有任何想法?

提前致谢:)

编辑:

我的dataSource方法是:

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return postsArray.count;
}

编辑#2:

我的代码我用来填充我的数组并重新加载我的tableView

PFQuery *findData = [PFQuery queryWithClassName:@"AllPosts"];
    [findData setLimit:1000];
    [findData findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error == nil) {
            for (PFObject *object in objects) {
                PFObject *post = object[@"content"];
                NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:post, nil];
                postsArray = array;
                [table reloadData];
            }
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable To Load" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];

2 个答案:

答案 0 :(得分:0)

您还需要检查是否实际返回了单元格对象。调用dequeueReusableCellWithIdentifier:

后,还要输入以下代码
if(!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

这可以解决您的问题

答案 1 :(得分:0)

你做错了,每当你在对象中找到PFObject时,你用新数据替换数组,你的代码应该是这样的: -

//first alloc your array 
 postsArray=[[NSMutableArray alloc] init];
//Logic goes here
    PFQuery *findData = [PFQuery queryWithClassName:@"AllPosts"];
        [findData setLimit:1000];
        [findData findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error == nil) {
                for (PFObject *object in objects) {
                    PFObject *post = object[@"content"];
                    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:post, nil];
                    [postsArray addObject:array];
                    [table reloadData];
                }
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable To Load" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
            }
        }];