使用自定义UITableViewCell的UITableView上单元格之间的间距/页边距



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

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

这是我的代码:

- (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(一个数组)中。

如果还不清楚,这里有一个关于视图控制器的Parse文档链接:https://parse.com/docs/ios_guide#ui-tables/iOS

您可以返回数组计数的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;
    }
}

最新更新