Tarun Seera
Tarun Seera

Reputation: 4402

Can I give space between UITableViewCell

I would like to give space of 10 pixel in each table view cell.

How I can do that?

Right now all cells are coming without any space

enter image description here

Here is my cell function

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *cellIdentifier = @"MyOrderCell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  NSLog(@"%d",indexPath.row);


 if (cell == nil)
 {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 }



 UILabel *orderid = (UILabel*)[cell.contentView viewWithTag:101];
 orderid.textColor = kMaroonColor;
 orderid.font = [UIFont fontWithName:kFontName size:kProductFont];
 orderid.text = [NSString stringWithFormat:@"ORDER ID : %@",contentDict[@"order_id"]];

 UILabel *date = (UILabel*)[cell.contentView viewWithTag:102];
 date.textColor = kMaroonColor;
 date.font = [UIFont fontWithName:kFontName size:kProductFont];

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 NSDate *date1 = [dateFormat dateFromString:contentDict[@"order_date"]];

 // Convert date object to desired output format
 [dateFormat setDateFormat:kDateFormat1];
 NSString *dateStr = [dateFormat stringFromDate:date1];
 date.text = dateStr;

 NSArray *products = contentDict[@"products"];
 UILabel *noOfProducts = (UILabel*)[cell.contentView viewWithTag:103];
 noOfProducts.textColor = kMaroonColor;
 noOfProducts.font = [UIFont fontWithName:kFontName size:kProductFont];
 noOfProducts.text= [NSString stringWithFormat:@"NO OF PRODUCTS : %d",products.count];


 NSArray *totalArray = contentDict[@"totals"];
 NSDictionary *totalDict = [totalArray objectAtIndex:0];
 UILabel *price = (UILabel*)[cell.contentView viewWithTag:104];
 price.textColor = [UIColor blackColor];
 price.font = [UIFont fontWithName:kFontName size:kProductFont];
 NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\u20B9 %@",totalDict[@"total"]]];
 [attributeString addAttribute:NSForegroundColorAttributeName
                        value:kGreyColor
                        range:NSMakeRange(0, 1)];
 price.attributedText = attributeString; 

 UIView* shadowView = [[UIView alloc]init];
 [cell setBackgroundView:shadowView];
 // border radius
 [shadowView.layer setCornerRadius:10.0f];

 // border
 [shadowView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
 [shadowView.layer setBorderWidth:1.5f];

 // drop shadow
 [shadowView.layer setShadowColor:[UIColor blackColor].CGColor];
 [shadowView.layer setShadowOpacity:0.8];
 [shadowView.layer setShadowRadius:3.0];
 //[cell.layer setShadowOffset:CGSizeMake(5.0, 5.0)];
 [tableView setSeparatorInset:UIEdgeInsetsMake(10, 10, 10, 10)];
 [cell setLayoutMargins:UIEdgeInsetsMake(10, 10, 10, 10)];
 //    [tableView setIndentationWidth:10];
 //    [tableView setIndentationLevel:2];

 //tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
 return cell;
}

Upvotes: 9

Views: 26724

Answers (13)

Dambar B. Bista
Dambar B. Bista

Reputation: 74

// I found this on internet hope this will help some future seeker
// This method should implement in custom class
// This for give space between row

override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        var frame =  newFrame
        frame.origin.y += 4
        frame.size.height -= 2 * 5
        super.frame = frame
    }
}

Upvotes: 0

pallavi
pallavi

Reputation: 513

Add UIView (childview) to ContentView. Add 10 pixcel spacing from top and bottom to parentView. Add your cell content in childView.

Upvotes: 0

jessi
jessi

Reputation: 1518

If you want to be able to have sections and do not want to create mystery space within your cells, create a collection view instead of a table view. Collection Views can be set to vertical only and one column only = you can also set the minimum line height and interitem spacing. Apple Developer Docs for UICollectionView

Upvotes: 0

Matthieu Riegler
Matthieu Riegler

Reputation: 54619

You can achieve that by having 1 cell per section & set tableview style to Grouped.

Upvotes: 0

Yagnesh Dobariya
Yagnesh Dobariya

Reputation: 2251

In the user interface for UITableView set Row Height Attribute value 210. and In the user interface for UITableViewCell set Row Height Attribute value 200.

It will put 10 px space between each cell in the TableView

Upvotes: 4

MudOnTire
MudOnTire

Reputation: 506

I guess the best way is to add a separator cell for each normal cell. let each kind of cell do its own job, and you can reuse the the separator and normal cell else where and need not to change their UI again. and you can customize the separator as you want, and all the calculations are easy, easy like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count * 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row % 2 == 0) {
        CellForData *cell = [tableView dequeueReusableCellWithIdentifier:@"dataCellID" forIndexPath:indexPath];
        return cell;
    }else{
        CellForSeparation *cell = [tableView dequeueReusableCellWithIdentifier:@"separatorCellID" forIndexPath:indexPath];
        return cell;
    }
}

Upvotes: 2

fathima
fathima

Reputation: 181

Increase the cell Hight.... And hide the separator and set the background view setBackgroundColor:[UIColor clearColor]]

Upvotes: 1

Himanshu jamnani
Himanshu jamnani

Reputation: 326

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
      // number of cells or array count
      return 10;
    }

- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section
    {
      return 1;
    }

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
      // space between cells
       return 10;
    }

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *view = [UIView new];
        [view setBackgroundColor:[UIColor clearColor]];
        return view;
    }

Upvotes: 6

Suresh Kumar Durairaj
Suresh Kumar Durairaj

Reputation: 2126

Create a container view inside UITableviewCell->ContentView. You keep all your subviews(labels,buttons,views) inside that container view. Now set container's boundaries away from the Cell's contentview by setting appropriate constraints.

UITableviewCell->ContentView->YourCustomContainerView-> (all subviews). Set YourCustomContainerView's boundaries away from the ContentView.

Upvotes: 2

fathima
fathima

Reputation: 181

its so simple you can set the auto layout or adjust the Hight of the label as well increases the hight of the cell

Upvotes: 1

Abhinandan Pratap
Abhinandan Pratap

Reputation: 2148

just make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return yourArry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return cellSpacingHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *v = [UIView new];
    [v setBackgroundColor:[UIColor clearColor]];
    return v;
}

Upvotes: 2

Chetan
Chetan

Reputation: 2124

If for a simple solution you can create sections for all cells. Each section each row. You can add a footer height for spacing between sections.

Upvotes: 1

Saheb Roy
Saheb Roy

Reputation: 5957

The most simple way would be make your cells height a bit bigger(3pt from top and 7pt from the bottom) than that of your cell's background image, and making the colour as [UIColor clearColor]. That would give the illusion that the cells have a 10 pt gap in between.

Upvotes: 12

Related Questions