How to give space between two cells in tableview?

前端 未结 26 1458
谎友^
谎友^ 2020-11-29 00:11

I want a space between two cell in table view,

I want cell like this,

\"enter<

相关标签:
26条回答
  • 2020-11-29 00:56

    For people that looking for alternative way of showing gaps between cells without using sections, you may want to show alternate colours and height like below. Use clearColor for the spacing.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
        if (indexPath.row % 2 == 1)
        {
            static NSString *CellIdentifier = @"cellID1";
            UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                reuseIdentifier:CellIdentifier];
            }
    
            cell.backgroundColor = [UIColor colorWhite];
    
            return cell;
    
        } else {
    
            static NSString *CellIdentifier2 = @"cellID2";
            UITableViewCell *cell2 = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
            if (cell2 == nil) {
                cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                reuseIdentifier:CellIdentifier2];
            }
            cell2.backgroundColor = [UIColor clearColor];
    
            return cell2;
        }
    
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row % 2 == 1) {
            return 40.0;
        } else {
            return 2.0;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 00:56

    Sometimes, you might really want to keep the tableview divided in rows, and have 1 section. For example, this could happen if you need to display a custom header for that table view that stays in place when you scroll through the section.

    What I would recommend doing in this case, is returning a bigger float than the normal height of a cell in:

    - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    and then making sure that the table style is Plain, and that the cell separator is none. You can do that in the XIB file itself, or in code:

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.style = UITableViewStylePlain;

    Maybe also add the cell's selection style to none (otherwise it will look like you are selecting more than just the visible part of the cell).

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    This will give the impression of space between the cells, but at the same time keeping them as rows in one section (which sometimes is what you want).

    0 讨论(0)
  • 2020-11-29 00:58

    I suggest to create a custom UITableViewCell base class and use this class like below,

    1. Create custom UITableViewCell class
    2. Create a UIView in new class, this will act as the 'baseContentView' and it will be immediate child of 'UITableViewCell.contentView'
    3. Adjust the top 'padding' on 'baseContentView' (this will be the separator/gap) from parent view (UITableViewCell.contentView)
    4. Inherit all your custom classes from this class rather than UITableViewCell
    5. Add all the content/subviews to 'baseContentView' rather than 'self.contentView'

    You can play with the 'padding' as per your need.

    0 讨论(0)
  • 2020-11-29 00:59

    The Best way to get space between two cells in TableView, declare the numbers of sections you want in delegate method of numberofsections this way

    For example you have array of 10 objects

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      return [array count]; //array count returns 10
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
        return 1;// this should be one because it will create space between two cells if you want space between 4 cells you can modify it.
    }
    

    Then the important point is in cellForRowAtIndexPath delegate method you need to use indexpath.section but not indexpath.row

    cell.textLabel.text=[NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.section]];
    

    That is is check your tableview for the space between two cells. Enjoy!

    0 讨论(0)
  • 2020-11-29 00:59

    Add these lines in the cellForRowAtIndexPath UITableViewDelegate method before returning the cell.

    let separator = UIView(frame: CGRectMake(0, 0, cell!.bounds.size.width, 1))
    separator.backgroundColor = UIColor.whiteColor()
    cell.contentView.addSubview(separator)
    
    0 讨论(0)
  • 2020-11-29 00:59
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    
        return __titlesArray.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 1;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 10;
    }
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *header = [[UIView alloc]init];
        header.backgroundColor = [UIColor clearColor];
        return header;
    
    }
    
    0 讨论(0)
提交回复
热议问题