I have a custom UITableViewCell that I'm using in my TableView, but I want there to be spacing between each cell. The info that gets put into the cell is being grabbed from my database (I'm using Parse), so it's different every time. I found this great SO solution here: Spacing between cells on UITableView with Custom UITableViewCell
but the problem after I implemented the code from that question is that now my tableview only shows every other cell, it skips over info in my array and doesn't display it.
Here is my code:
- (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 is the array in which the info from the database goes into. How can I make it so there is still spacing between cells, but no information in the array is skipped?
EDIT: Here is the code that fetches the data
- (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;
}
This query adds the objects to self.objects, an array.
If it is still unclear, here is a link to the Parse docs regarding the view controller: https://parse.com/docs/ios_guide#ui-tables/iOS