Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine margin of a grouped UITableView (or better, how to set it)?

Tags:

uitableview

The grouped UITableView places a margin between the edge of the view and the table cells. Annoyingly (for me) this margin is some function of the width of the view.

In my application I have two UITableViews of different widths that I am looking to align the cell edges of.

Is it possible to get this margin? Or better is it possible to set this margin?

cheers, --Ben

like image 856
user577814 Avatar asked Jan 16 '11 21:01

user577814


People also ask

What is grouped Uitableview?

Style. grouped. A table view where sections have distinct groups of rows.

How do you remove the blank space at the top of a grouped Uitableview?

You can change the space between tableviewcells themselves by: [myTableView setSectionHeaderHeight:0]; [myTableView setSectionFooterHeight:25];


3 Answers

Grouped TableView Width in Relation to Cell Margin

TBWidth (0 to 20) Left Margin = TBWidth - 10

TBWidth (20 to 400) Left Margin = 10

TBWidth (401 to 546) Left Margin = 31

TBWidth (547 to 716) Left Margin = apx 6% of tableView

TBWidth (717 to 1024) Left Margin = 45

- (float) groupedCellMarginWithTableWidth:(float)tableViewWidth
{
    float marginWidth;
    if(tableViewWidth > 20)
    {
        if(tableViewWidth < 400)
        {
            marginWidth = 10;
        }
        else
        {
            marginWidth = MAX(31, MIN(45, tableViewWidth*0.06));
        }
    }
    else
    {
        marginWidth = tableViewWidth - 10;
    }
    return marginWidth;
}
like image 84
Matthew Thomas Avatar answered Nov 03 '22 17:11

Matthew Thomas


By creating (and using!) a subclass of UITableViewCell, you can possibly achieve what you're looking for. Just move the contentview and backgroundview around in layoutsubviews, as in the code sample below which shifts the visible parts of the cell 20pt to the right.

- (void) layoutSubviews
{
    NSLog (@"Cell/contentview/backgroundview before:\n%@\n%@\n%@", self, self.contentView, self.backgroundView);
    [super layoutSubviews];
    CGRect frame = self.backgroundView.frame;
    frame.origin.x += 20;
    frame.size.width -= 20;
    self.backgroundView.frame = frame;
    frame = self.contentView.frame;
    frame.origin.x += 20;
    frame.size.width -= 20;
    self.contentView.frame = frame;
    
    NSLog (@"Cell/contentview/backgroundview after:\n%@\n%@\n%@", self, self.contentView, self.backgroundView);
}
like image 29
Steven Kramer Avatar answered Nov 03 '22 15:11

Steven Kramer


I was using Matthew Thomas implementation but it's unreliable (it's broken if you use autorotation in your controllers) and has a lot of hardcoded code... I realized that there is a very simple solution, my implementation is a single line of code:

- (float)cellMargins
{
    return self.backgroundView.frame.origin.x * 2;
}

This is far better IMO :)

EDIT: my implementation was unreliable too (it does not work properly when switching to/from editing mode), this is my final implementation (I handled right and left margins separately):

- (float)leftMargin
{
    return self.contentView.frame.origin.x;
}

- (float)rightMargin
{
    CGRect frame = self.contentView.frame;
    float containerWidth = frame.size.width;
    float margin = self.frame.size.width - (containerWidth + frame.origin.x);
    return margin;
}

- (float)cellMargins
{
    return ([self leftMargin] + [self rightMargin]);
}
like image 12
daveoncode Avatar answered Nov 03 '22 15:11

daveoncode