S
S
SVM122014-09-25 10:45:59
iOS
SVM12, 2014-09-25 10:45:59

How to programmatically change the width of a UITableView?

Hello.
In general, there is a UIViewController in which I placed a UITableView in full screen (main UIView). iPad. I use a storyboard.
There is a task: when an entry is selected in a UITableView, the size and position of the UItableView should change (the width decreases, and the view itself shifts to the right). And a view (or without it) with the details of the selected record should be displayed on the left.
a54aede09f7e46d3b6da34e9372edece.jpg
I tried to do it in the following way:
Controller code:

- (void)viewDidLoad {
    [super viewDidLoad];
    bigFrame = self.tableView.frame;
    CGFloat xOffset = 256;
    smallFrame = CGRectMake(bigFrame.origin.x + xOffset, bigFrame.origin.y, bigFrame.size.width - xOffset, bigFrame.size.height);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.frame = smallFrame;
}

The problem is that in this case it only works on the second selection attempt.
Please explain how to do this behavior in the interface. SplitViewController is not used.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ifau, 2014-09-25
@SVM12

You can move the tableView Leading Space to Superview to Outlet, and change it

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.leftSpace.constant = 256;
    [UIView animateWithDuration:animationDuration animations:^
    {
        [self.view layoutIfNeeded];
    }];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.leftSpace.constant = 0;
    [UIView animateWithDuration:animationDuration animations:^
    {
        [self.view layoutIfNeeded];
    }];
}

S
SVM12, 2014-09-25
@SVM12

ifau, Thank you. Helped.
I also found out that if the tableView is created programmatically via initWithFrame, then the problem described above does not arise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question