UITableviewCell height according to In side another UITableviewCell height











up vote
0
down vote

favorite












{
MainTitle 1
{
Subtitle 1
( item 1
item 2
item 3 )
},
{
MainTitle 2
{
Subtitle 2
( item 1
item 2
item 3)
}
}



There is JSON data format that I have to display in UITableview. The data is restaurant's menu. I create Table 1 for MainTitle display and Table 1 cell inside another UITableview Table 2 for Subtitle and item Subtitle set as header and item as row.



Tableview cell height is dynamic so I set it UITableViewAutomaticDimension for both tableview. Table 1 cell height is set according to inside it Table 2 height. Table 2 is not scrollable, it's height is according to content height.



So, I want to set Table 1 cell height after loading Table 2 all data.










share|improve this question
























  • I think nested table view isn't a good idea. You should try to handle them with only 1 UITableView
    – trungduc
    Nov 20 at 4:42















up vote
0
down vote

favorite












{
MainTitle 1
{
Subtitle 1
( item 1
item 2
item 3 )
},
{
MainTitle 2
{
Subtitle 2
( item 1
item 2
item 3)
}
}



There is JSON data format that I have to display in UITableview. The data is restaurant's menu. I create Table 1 for MainTitle display and Table 1 cell inside another UITableview Table 2 for Subtitle and item Subtitle set as header and item as row.



Tableview cell height is dynamic so I set it UITableViewAutomaticDimension for both tableview. Table 1 cell height is set according to inside it Table 2 height. Table 2 is not scrollable, it's height is according to content height.



So, I want to set Table 1 cell height after loading Table 2 all data.










share|improve this question
























  • I think nested table view isn't a good idea. You should try to handle them with only 1 UITableView
    – trungduc
    Nov 20 at 4:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











{
MainTitle 1
{
Subtitle 1
( item 1
item 2
item 3 )
},
{
MainTitle 2
{
Subtitle 2
( item 1
item 2
item 3)
}
}



There is JSON data format that I have to display in UITableview. The data is restaurant's menu. I create Table 1 for MainTitle display and Table 1 cell inside another UITableview Table 2 for Subtitle and item Subtitle set as header and item as row.



Tableview cell height is dynamic so I set it UITableViewAutomaticDimension for both tableview. Table 1 cell height is set according to inside it Table 2 height. Table 2 is not scrollable, it's height is according to content height.



So, I want to set Table 1 cell height after loading Table 2 all data.










share|improve this question















{
MainTitle 1
{
Subtitle 1
( item 1
item 2
item 3 )
},
{
MainTitle 2
{
Subtitle 2
( item 1
item 2
item 3)
}
}



There is JSON data format that I have to display in UITableview. The data is restaurant's menu. I create Table 1 for MainTitle display and Table 1 cell inside another UITableview Table 2 for Subtitle and item Subtitle set as header and item as row.



Tableview cell height is dynamic so I set it UITableViewAutomaticDimension for both tableview. Table 1 cell height is set according to inside it Table 2 height. Table 2 is not scrollable, it's height is according to content height.



So, I want to set Table 1 cell height after loading Table 2 all data.







objective-c uitableview ios-autolayout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 7:01









Mukesh

465316




465316










asked Nov 20 at 4:31









Rahul Parikh

1819




1819












  • I think nested table view isn't a good idea. You should try to handle them with only 1 UITableView
    – trungduc
    Nov 20 at 4:42


















  • I think nested table view isn't a good idea. You should try to handle them with only 1 UITableView
    – trungduc
    Nov 20 at 4:42
















I think nested table view isn't a good idea. You should try to handle them with only 1 UITableView
– trungduc
Nov 20 at 4:42




I think nested table view isn't a good idea. You should try to handle them with only 1 UITableView
– trungduc
Nov 20 at 4:42












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You could use proper UITableView sections and rows. Add "MainTitle" as Table Section and MainTitle's "Items" as Section Rows.






share|improve this answer





















  • what about "Subtitle"
    – Rahul Parikh
    Nov 20 at 9:07


















up vote
0
down vote













I'd recommend using one single tableview as mentioned by @trungduc and @Raj D



You should be able to use a custom tableview cell to layout the items the way you want, and you can always use buttons for items that need to be clickable (i.e. if you need to click on the menu items and show another view controller).



Here's a basic mockup of what I meanenter image description here:



You'll want to use a stack view within the cell and then dynamically generate the menu items based off your backing data model (this should allow it to obtain the correct intrinsic content size for UITableViewAutomaticDimension to size the cells correctly).



enter image description here



Here's an example of what I mean by dynamically adding the menu items when setting the submenu item of the custom cell:



- (void)setupMenuItemsStack:(NSArray <RPMenuItem *> *)menuItems {
for (UIView *subview in self.menuItemsStack.arrangedSubviews) {
[NSLayoutConstraint deactivateConstraints:subview.constraints];
[subview removeFromSuperview];
}
for (RPMenuItem *menuItem in menuItems) {
UIButton *menuItemButton = [UIButton buttonWithType:UIButtonTypeSystem];
[menuItemButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[menuItemButton setTitle:menuItem.title forState:UIControlStateNormal];
[self.menuItemsStack addArrangedSubview:menuItemButton];
}
}


Depending on how you're structuring your model, the tableview methods would look something along these lines:



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.menuList.menus.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.menuList.menus objectAtIndex:section] subMenus].count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[self.menuList.menus objectAtIndex:section] title];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RPMenuTableViewCell *cell = (RPMenuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RPMenuCell" forIndexPath:indexPath];
RPSubMenu *subMenu = [[[self.menuList.menus objectAtIndex:indexPath.section] subMenus] objectAtIndex:indexPath.row];
[cell setSubMenu:subMenu];
return cell;
}


Where the menu list is a top level list of menus and it cascades down from there (i.e. menu list holds an array of menus, menus hold an array of submenus, submenus hold an array of menu items).



@property (strong, nullable) RPMenuList *menuList;


Adding the ability to expand and contract (accordion style) to show the menu items would probably be a nice touch as well:



enter image description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386278%2fuitableviewcell-height-according-to-in-side-another-uitableviewcell-height%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You could use proper UITableView sections and rows. Add "MainTitle" as Table Section and MainTitle's "Items" as Section Rows.






    share|improve this answer





















    • what about "Subtitle"
      – Rahul Parikh
      Nov 20 at 9:07















    up vote
    0
    down vote













    You could use proper UITableView sections and rows. Add "MainTitle" as Table Section and MainTitle's "Items" as Section Rows.






    share|improve this answer





















    • what about "Subtitle"
      – Rahul Parikh
      Nov 20 at 9:07













    up vote
    0
    down vote










    up vote
    0
    down vote









    You could use proper UITableView sections and rows. Add "MainTitle" as Table Section and MainTitle's "Items" as Section Rows.






    share|improve this answer












    You could use proper UITableView sections and rows. Add "MainTitle" as Table Section and MainTitle's "Items" as Section Rows.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 at 7:11









    Raj D

    27110




    27110












    • what about "Subtitle"
      – Rahul Parikh
      Nov 20 at 9:07


















    • what about "Subtitle"
      – Rahul Parikh
      Nov 20 at 9:07
















    what about "Subtitle"
    – Rahul Parikh
    Nov 20 at 9:07




    what about "Subtitle"
    – Rahul Parikh
    Nov 20 at 9:07












    up vote
    0
    down vote













    I'd recommend using one single tableview as mentioned by @trungduc and @Raj D



    You should be able to use a custom tableview cell to layout the items the way you want, and you can always use buttons for items that need to be clickable (i.e. if you need to click on the menu items and show another view controller).



    Here's a basic mockup of what I meanenter image description here:



    You'll want to use a stack view within the cell and then dynamically generate the menu items based off your backing data model (this should allow it to obtain the correct intrinsic content size for UITableViewAutomaticDimension to size the cells correctly).



    enter image description here



    Here's an example of what I mean by dynamically adding the menu items when setting the submenu item of the custom cell:



    - (void)setupMenuItemsStack:(NSArray <RPMenuItem *> *)menuItems {
    for (UIView *subview in self.menuItemsStack.arrangedSubviews) {
    [NSLayoutConstraint deactivateConstraints:subview.constraints];
    [subview removeFromSuperview];
    }
    for (RPMenuItem *menuItem in menuItems) {
    UIButton *menuItemButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [menuItemButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [menuItemButton setTitle:menuItem.title forState:UIControlStateNormal];
    [self.menuItemsStack addArrangedSubview:menuItemButton];
    }
    }


    Depending on how you're structuring your model, the tableview methods would look something along these lines:



    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.menuList.menus.count;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.menuList.menus objectAtIndex:section] subMenus].count;
    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[self.menuList.menus objectAtIndex:section] title];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RPMenuTableViewCell *cell = (RPMenuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RPMenuCell" forIndexPath:indexPath];
    RPSubMenu *subMenu = [[[self.menuList.menus objectAtIndex:indexPath.section] subMenus] objectAtIndex:indexPath.row];
    [cell setSubMenu:subMenu];
    return cell;
    }


    Where the menu list is a top level list of menus and it cascades down from there (i.e. menu list holds an array of menus, menus hold an array of submenus, submenus hold an array of menu items).



    @property (strong, nullable) RPMenuList *menuList;


    Adding the ability to expand and contract (accordion style) to show the menu items would probably be a nice touch as well:



    enter image description here






    share|improve this answer



























      up vote
      0
      down vote













      I'd recommend using one single tableview as mentioned by @trungduc and @Raj D



      You should be able to use a custom tableview cell to layout the items the way you want, and you can always use buttons for items that need to be clickable (i.e. if you need to click on the menu items and show another view controller).



      Here's a basic mockup of what I meanenter image description here:



      You'll want to use a stack view within the cell and then dynamically generate the menu items based off your backing data model (this should allow it to obtain the correct intrinsic content size for UITableViewAutomaticDimension to size the cells correctly).



      enter image description here



      Here's an example of what I mean by dynamically adding the menu items when setting the submenu item of the custom cell:



      - (void)setupMenuItemsStack:(NSArray <RPMenuItem *> *)menuItems {
      for (UIView *subview in self.menuItemsStack.arrangedSubviews) {
      [NSLayoutConstraint deactivateConstraints:subview.constraints];
      [subview removeFromSuperview];
      }
      for (RPMenuItem *menuItem in menuItems) {
      UIButton *menuItemButton = [UIButton buttonWithType:UIButtonTypeSystem];
      [menuItemButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
      [menuItemButton setTitle:menuItem.title forState:UIControlStateNormal];
      [self.menuItemsStack addArrangedSubview:menuItemButton];
      }
      }


      Depending on how you're structuring your model, the tableview methods would look something along these lines:



      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      return self.menuList.menus.count;
      }

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return [[self.menuList.menus objectAtIndex:section] subMenus].count;
      }

      - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      return [[self.menuList.menus objectAtIndex:section] title];
      }

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      RPMenuTableViewCell *cell = (RPMenuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RPMenuCell" forIndexPath:indexPath];
      RPSubMenu *subMenu = [[[self.menuList.menus objectAtIndex:indexPath.section] subMenus] objectAtIndex:indexPath.row];
      [cell setSubMenu:subMenu];
      return cell;
      }


      Where the menu list is a top level list of menus and it cascades down from there (i.e. menu list holds an array of menus, menus hold an array of submenus, submenus hold an array of menu items).



      @property (strong, nullable) RPMenuList *menuList;


      Adding the ability to expand and contract (accordion style) to show the menu items would probably be a nice touch as well:



      enter image description here






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        I'd recommend using one single tableview as mentioned by @trungduc and @Raj D



        You should be able to use a custom tableview cell to layout the items the way you want, and you can always use buttons for items that need to be clickable (i.e. if you need to click on the menu items and show another view controller).



        Here's a basic mockup of what I meanenter image description here:



        You'll want to use a stack view within the cell and then dynamically generate the menu items based off your backing data model (this should allow it to obtain the correct intrinsic content size for UITableViewAutomaticDimension to size the cells correctly).



        enter image description here



        Here's an example of what I mean by dynamically adding the menu items when setting the submenu item of the custom cell:



        - (void)setupMenuItemsStack:(NSArray <RPMenuItem *> *)menuItems {
        for (UIView *subview in self.menuItemsStack.arrangedSubviews) {
        [NSLayoutConstraint deactivateConstraints:subview.constraints];
        [subview removeFromSuperview];
        }
        for (RPMenuItem *menuItem in menuItems) {
        UIButton *menuItemButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [menuItemButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [menuItemButton setTitle:menuItem.title forState:UIControlStateNormal];
        [self.menuItemsStack addArrangedSubview:menuItemButton];
        }
        }


        Depending on how you're structuring your model, the tableview methods would look something along these lines:



        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return self.menuList.menus.count;
        }

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [[self.menuList.menus objectAtIndex:section] subMenus].count;
        }

        - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return [[self.menuList.menus objectAtIndex:section] title];
        }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        RPMenuTableViewCell *cell = (RPMenuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RPMenuCell" forIndexPath:indexPath];
        RPSubMenu *subMenu = [[[self.menuList.menus objectAtIndex:indexPath.section] subMenus] objectAtIndex:indexPath.row];
        [cell setSubMenu:subMenu];
        return cell;
        }


        Where the menu list is a top level list of menus and it cascades down from there (i.e. menu list holds an array of menus, menus hold an array of submenus, submenus hold an array of menu items).



        @property (strong, nullable) RPMenuList *menuList;


        Adding the ability to expand and contract (accordion style) to show the menu items would probably be a nice touch as well:



        enter image description here






        share|improve this answer














        I'd recommend using one single tableview as mentioned by @trungduc and @Raj D



        You should be able to use a custom tableview cell to layout the items the way you want, and you can always use buttons for items that need to be clickable (i.e. if you need to click on the menu items and show another view controller).



        Here's a basic mockup of what I meanenter image description here:



        You'll want to use a stack view within the cell and then dynamically generate the menu items based off your backing data model (this should allow it to obtain the correct intrinsic content size for UITableViewAutomaticDimension to size the cells correctly).



        enter image description here



        Here's an example of what I mean by dynamically adding the menu items when setting the submenu item of the custom cell:



        - (void)setupMenuItemsStack:(NSArray <RPMenuItem *> *)menuItems {
        for (UIView *subview in self.menuItemsStack.arrangedSubviews) {
        [NSLayoutConstraint deactivateConstraints:subview.constraints];
        [subview removeFromSuperview];
        }
        for (RPMenuItem *menuItem in menuItems) {
        UIButton *menuItemButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [menuItemButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [menuItemButton setTitle:menuItem.title forState:UIControlStateNormal];
        [self.menuItemsStack addArrangedSubview:menuItemButton];
        }
        }


        Depending on how you're structuring your model, the tableview methods would look something along these lines:



        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return self.menuList.menus.count;
        }

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [[self.menuList.menus objectAtIndex:section] subMenus].count;
        }

        - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return [[self.menuList.menus objectAtIndex:section] title];
        }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        RPMenuTableViewCell *cell = (RPMenuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RPMenuCell" forIndexPath:indexPath];
        RPSubMenu *subMenu = [[[self.menuList.menus objectAtIndex:indexPath.section] subMenus] objectAtIndex:indexPath.row];
        [cell setSubMenu:subMenu];
        return cell;
        }


        Where the menu list is a top level list of menus and it cascades down from there (i.e. menu list holds an array of menus, menus hold an array of submenus, submenus hold an array of menu items).



        @property (strong, nullable) RPMenuList *menuList;


        Adding the ability to expand and contract (accordion style) to show the menu items would probably be a nice touch as well:



        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 19:35

























        answered Nov 21 at 18:16









        R4N

        75224




        75224






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386278%2fuitableviewcell-height-according-to-in-side-another-uitableviewcell-height%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks