Iphone 自定义UITableViewCell的UITableView上单元格之间的间距/边距

Iphone 自定义UITableViewCell的UITableView上单元格之间的间距/边距,iphone,objective-c,cocoa-touch,uitableview,Iphone,Objective C,Cocoa Touch,Uitableview,我在TableView中使用了一个自定义UITableViewCell,但我希望每个单元格之间都有间距。放入单元格的信息是从我的数据库中抓取的(我正在使用Parse),所以每次都不同。我在这里找到了一个很棒的SO解决方案: 但在我实现了这个问题的代码之后,问题是现在我的tableview只显示其他单元格,它跳过数组中的信息,而不显示它 这是我的密码: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NS

我在TableView中使用了一个自定义UITableViewCell,但我希望每个单元格之间都有间距。放入单元格的信息是从我的数据库中抓取的(我正在使用Parse),所以每次都不同。我在这里找到了一个很棒的SO解决方案:

但在我实现了这个问题的代码之后,问题是现在我的tableview只显示其他单元格,它跳过数组中的信息,而不显示它

这是我的密码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 1)
        return 40;
    return 73;

    //return 73;
}

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

    //Begin
    static NSString *CELL_ID2 = @"SOME_STUPID_ID2";
    // even rows will be invisible
    if (indexPath.row % 2 == 1)
    {
        UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];

        if (cell2 == nil)
        {
            cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:CELL_ID2];
            [cell2.contentView setAlpha:0];
            [cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
        }
        return cell2;
    }

    //End

    static NSString *CellIdentifier = @"debateCell";

    RipDebateTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RipDebateTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }



    // Configure the cell
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    //[df setLocale:enUSPOSIXLocale];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    [df setDateFormat:@"MMM dd, yyyy "];
    NSString *my = [df stringFromDate:[object objectForKey:@"Date"]];
    NSLog(@"%@", my);
    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.titleOf.text = [object objectForKey:@"Topic"];
    cell.dateOf.text = [NSString stringWithFormat:@"Date: %@", my];
    cell.typeOf.text = @"Debate";
    cell.cellTop.image = [UIImage imageNamed:@"table-top"];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}
self.objects是数据库中的信息进入的数组。如何使单元格之间仍有间距,但不跳过数组中的任何信息

编辑:下面是获取数据的代码

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    //query whereKey:@"school" equalTo:[PFUser curr]
    [query whereKey:@"school" equalTo:[[PFUser currentUser]objectForKey:@"mySchool"]];
    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    if ([self.objects count] == 0) {
        //[query whereKey:@"providerZipCode" equalTo:[NSNumber numberWithInt:78664]];
        NSLog(@"NONE");
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    [query orderByAscending:@"Date"];

    return query;
}
此查询将对象添加到self.objects数组中


如果仍然不清楚,这里有一个指向有关视图控制器的解析文档的链接:

您可以返回数组计数的2倍(在numberOfRowsInSection中),并在cellForRowAtIndexPath中执行类似操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 1 ) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DataCell" forIndexPath:indexPath];
        cell.textLabel.text = self.theData[(indexPath.row -1)/2];
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SpacerCell" forIndexPath:indexPath];
        return cell;
    }
}

只需要奇数行就可以填充数据,因为偶数为空。右侧,偶数行是奇数行之间的边距,使其看起来像是有间距。我的问题是我上面的代码没有显示我的对象数组中的所有内容…每次调用cellForRowAtIndexPath时,一个PFObject被发送到该方法中,因此当它是偶数行时,该对象被忽略,并且不会发生任何事情。你知道,该方法每行调用一次,也就是说tableview中缺少数据,是的,正如rdelmar所说,你可以为每一行放置一个空的headerview尝试另一种方法,我发布了CellForRowIndexPath方法,该方法已将该单元格的对象与该方法一起发送。换句话说,每个单元格在数组self.objects中预先指定了一个对象。我正在使用parse的PFQueryTableViewController的子类..@user2584268,我不理解你的评论。我发布的代码将访问子句if部分中数组的每个元素。你说你有一个数组,objects,但我不知道你是如何使用它的。很抱歉给你带来困惑。如果查看我的方法头:-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath对象:(PFObject*)对象{其中已经有一个对象,我用它来填充每个单元格的信息。@user2584268,如何传递该对象?@user2584268,如果由于数据传递方式的原因,这种方法不起作用,为什么不使用完全不同的方法,在单元格中有一个内部视图,其中任何子视图都需要显示数据被嵌入,并使单元格的一部分空白。这将使您的单元格看起来周围有一个间隔。