0

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

Community
  • 1
  • 1
Jacob
  • 2,338
  • 2
  • 22
  • 39
  • u want only odd rows wants to fill up the data, where as evens are left blank .. – Shankar BS Dec 03 '13 at 04:35
  • Right, the even rows are the margins between the odd rows to make it look like there is spacing. My issue is that my code above isn't displaying everything in my objects array....Each time cellForRowAtIndexPath is called, a PFObject is being sent into the method, so when it's an even row, that object is neglected and nothing happens with it. – Jacob Dec 03 '13 at 04:44
  • this method is calling once per row as u know, thats wy there are missing the data in the tableview, ya as rdelmar said u can put a empty headerview for each row – Shankar BS Dec 03 '13 at 05:54
  • try out another approach that i posted – Shankar BS Dec 03 '13 at 06:15

1 Answers1

0

You can return 2 times the count of your array (in numberOfRowsInSection), and do something like this in 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;
    }
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • My cellForRowAtIndexPath method already has the object for that cell sent in with the method. In other words, each cell is pre-assigned an object in the array self.objects. I'm using a subclass of parse's PFQueryTableViewController.. – Jacob Dec 03 '13 at 05:12
  • @user2584268, I don't understand your comment. The code I posted will access every element of your array in the if part of the clause. You say you have an array, objects, but I don't see how you're using it. – rdelmar Dec 03 '13 at 05:15
  • sorry for the confusion. If you look at my method header: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { there is an object in it already, which is what I use to fill the info of each cell. – Jacob Dec 03 '13 at 05:31
  • @user2584268, how do you pass that object in? – rdelmar Dec 03 '13 at 05:37
  • @user2584268, If this approach doesn't work because of the way you pass in the data, why not go to a completely different approach, where you have an inner view in your cell, where any subviews you need to display the data are embedded, and make the part of the cell blank white space. This will make it look like your cells have a spacer around them. – rdelmar Dec 03 '13 at 05:52
  • that method is one in the docs on Parse's website. They have made it easier to query a table by directly implementing each object into the cellForRowAtIndexPath method. I wouldn't really like to stray from that though. Is there another way I could accomplish what I want? – Jacob Dec 03 '13 at 23:47
  • Also, my app crashes when I use the numberOfRowsInSection method b/c the data is still being gathered from parse. Is there another way I could accomplish what I want? – Jacob Dec 03 '13 at 23:54
  • @user2584268 I don't know of another way, but I haven't worked with Parse, so I'm not familiar with the way they pass in the data, and how to work with that. Where is the data? Is it in an array? It would be helpful if you edited your question to show the way the data source works. – rdelmar Dec 03 '13 at 23:54
  • Yes, it's in an array: objects. It's an instance variable of the class. I will post the code in the question.. – Jacob Dec 04 '13 at 00:01
  • @user2584268, it looks like to me that the only way (if you want to continue to use tableView:cellForRowAtIndexPath:object:) is to add white space to the cell, as I suggested. Is there some reason you don't want to do that? – rdelmar Dec 04 '13 at 00:07
  • The only reason I wouldn't want to do that is I wouldn't want the user to be able to tap that white space to select the cell. Is there a way to make a "selectable" frame of the cell? – Jacob Dec 04 '13 at 00:11
  • @user2584268, you might be able to add a gesture recognizer to the inner view, and in its action method, do what ever you would normally do in didSelectRowAtIndexPath. You would have to set allowsSelection for your table view to NO, to turn off the normal selection behavior. – rdelmar Dec 04 '13 at 00:26