Nick Peachey
Nick Peachey

Reputation: 21

IOS UITableViewCell Spacer (or margin)

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];

Upvotes: 2

Views: 8380

Answers (3)

WaaleedKhan
WaaleedKhan

Reputation: 685

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

Upvotes: 0

A-Live
A-Live

Reputation: 8944

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.

Upvotes: 1

Ashley Mills
Ashley Mills

Reputation: 53111

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]

Upvotes: 0

Related Questions