2

I'm trying to get a space between my table custom view cells on my table view controller. For example, for each cell created it is stacked up one by one and there is no margin. For example on the facebook iPhone app has a spacer after each cell which i would like to create, any ideas guys?


Edit: From comment below

The code looks like this

MSRSalesCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
cell.backgroundView = [[CustomCellBackground alloc] init]; 
NSDictionary *company = [companies objectAtIndex:[indexPath row]]; 
cell.companyName.text = [company objectForKey:@"Name"]; 
cell.backgroundColor = [UIColor clearColor]; 
cell.backgroundView.bounds = CGRectMake(0, 0, 320, 55); 
[self loadImageAsync:cell withImageUrl:imageURL];
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Nick Peachey
  • 21
  • 1
  • 2

3 Answers3

1

Check my answer for the similar question, it suggests you to create the invisible cells between the cells you want to divide with some space.

Option 2 is to create only the visible cells, make their height above the visible area and prepare special content and selected view's, note the selected view creation is not so easy and you'll need to do it as you probably don't want the spacing area to get selected, so i'm using the first option when there's a need to get some cell's spacing.

The main (and probably the only) disadvantage of the first option is that you have to treat the cell's indexes in a special way to distinguish the spacing-cells and the visible-cells.

Community
  • 1
  • 1
A-Live
  • 8,904
  • 2
  • 39
  • 74
0

If your tableView's rowHeight is 50 points, make your background image view height say, 40 points, and centre it in the cell. Then make your cell background colour [UIcolor clearColor]

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • Hi, tried that, no change, still no space between each cell. The code looks like this MSRSalesCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; cell.backgroundView = [[CustomCellBackground alloc] init]; NSDictionary *company = [companies objectAtIndex:[indexPath row]]; cell.companyName.text = [company objectForKey:@"Name"]; cell.backgroundColor = [UIColor clearColor]; cell.backgroundView.bounds = CGRectMake(0, 0, 320, 55); [self loadImageAsync:cell withImageUrl:imageURL]; – Nick Peachey Jul 11 '12 at 11:23
  • Hmmm... has your background image been resized to fill the cell? – Ashley Mills Jul 11 '12 at 11:43
  • I dont use a background image, its just a custom UITableViewCell that has a number of UILabels laid out in a particular way. – Nick Peachey Jul 11 '12 at 11:55
  • What I'm trying to get at is that if you use an image view as your cell background that is smaller than the cell, it will give the impression of a margin between cells. – Ashley Mills Jul 11 '12 at 12:53
  • And lay it out in a xib file so you can see what you're doing, rather than trying to do it in code!! – Ashley Mills Jul 11 '12 at 12:55
  • thanks guys, going with the indexPath.row % 2 == 1 option seemed to work, great stuff – Nick Peachey Jul 11 '12 at 18:38
0

Okayy i did some thing like this.....

First the BGColor Of view which will hold the Tableview is set to White (it was a personal choice w.r.t to my design) and then the cellForRowAtIndexPath method looks some thing like this.

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;
    cell = nil;

    //Create Cell
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    
    }

    //Retrive Values From DB
    DBTable_Description *dbObject = [[DBTable_Description alloc]init];
    dbObject = [contentList objectAtIndex:[indexPath row]];
    //Cell View
    UIView *cellView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 65)];
    //ImageView
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(4.0, 8.0, 300.0, 58.0)];
    imageView.userInteractionEnabled = NO;
    imageView.image = imgForCellBkg;
    //Label
    UILabel *lblFor = [[UILabel alloc]initWithFrame:CGRectMake(70, 25, 200, 21)];
    lblFor.text =dbObject.field1;
    lblFor.backgroundColor = [UIColor clearColor];
    lblFor.font = [UIFont fontWithName:@"Helvetica Neue" size:16];
    lblFor.textColor = [UIColor grayColor];
    lblFor.shadowColor = [UIColor grayColor];

    //Adding Views to Cell View
    [cellView addSubview:imageView];
    [cellView addSubview:lblFor];

    [cell.contentView addSubview:cellView];



    cell.selectionStyle = NO;
    return cell;


}

Okay First and formost neglect the Database code. Now what i did is i created a View on each Cell name cellView (set the height as per ur requirement) next i had an Image View and set the appropriate image (again it may not be the thing u want to do) but pay attention to the CGRectMake i se the values according to the amount of gap i want b.w my cells.. the rest is usual.

here is the image of the view i had in my AppSee What i meant....???

Let me Know if it worked Cheers W

WaaleedKhan
  • 685
  • 7
  • 18
  • Using your example i have the below code, still doesnt make a space however :( cell.backgroundView = [[CustomCellBackground alloc] init]; cell.companyName.text = [company objectForKey:@"Name"]; cell.backgroundColor = [UIColor clearColor]; cell.backgroundView.bounds = CGRectMake(0, 0, 320, 55); [self loadImageAsync:cell withImageUrl:imageURL]; UIView *cellView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)]; [cell.contentView addSubview:cellView]; cell.selectionStyle = NO; return cell; – Nick Peachey Jul 11 '12 at 12:14
  • okayy If i m not wrong u have another class which actually draws the Cell. What u need to do is First Create The UIView and then the Cell. The View will start from 0,0 and the cell will have X and Y points according to how much u padding u want. You have Set X and Y of both Views to 0,0 so it is over lapping on each other and u cant see the padding – WaaleedKhan Jul 11 '12 at 12:24