UITableviewCell height according to In side another UITableviewCell height - objective-c

{
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.

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

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 mean:
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).
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:

Related

How to get the Array Index Value and showing them in label in collection view

i taking photos and showing them in scrolling part in collection view.i want to show the numbers like if that collection view has one image , i want to show 1.if there are 3 three images then i need to show 3,2, 1,for corresponding images .because I'm adding the latest picture at first .the numbering should be descending .Please help me to do this.
wat i did is ..
NSUInteger index;
for (id item in self.myimagearray)
{
index = [[self.myimagearray indexOfObject:self.myimagearray];
NSLog(#" the index are:%lu",(unsigned long)index);
Cell.waterMark_lbl.text = [NSString stringWithFormat:#"%lu",(unsigned long)index];
}
and it showing the count like 3 in all images .Please help me do this ..
note :you have to use custom collection view cell.
create a custom collection view cell with a label and with a image view.
Then create IBOutlet s to custom collection view cell class ,.h file.
then call for collection view delegate methods. like below.
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.yourarray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCutomCollectionViewCell *thecell = [collectionView dequeueReusableCellWithReuseIdentifier:#"your_cell_identifier" forIndexPath:indexPath];
//this is how you can get the index path to string
NSString *the_index_path = [NSString stringWithFormat:#"%li", (long)indexPath.row];
//then bind it to your label
thecell.mylabel.text = the_index_path;
//and bind your image data here
}

Collectionview cell to open new view

I have a collection view with 6 cells in it how do I have each cell open a new viewcontroller when clicked?
In order to achieve your goal you need to edit 2 files:
In the Storyboard you need to add a cell view for each static cell in your Collection View. Just drag as many UICollectionViewCells onto the Collection View. For each cell you need to define a unique reusable identifier (meaning a name like CellType1) in the Attributes Inspector when a cell is selected in the Storyboard's Document Outline. And then control-drag from each cell to the desired target view controller to create a Push segue (provided that your collection view is associated with a UINavigationController).
Create a subclass of UICollectionViewController and assign it in the Storyboard's as the Collection View Controller's class (see the Identity Inspector).
In your UICollectionViewController subclass implement the following methods:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// Enter the number of static cells that are present in the Storyboard's collection view:
return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Enter the reusable identifiers that are defined for each cell in the Storyboard's collection view:
NSArray *cellIdentifiers = #[#"CellType1", #"CellType2", #"CellType3"];
NSInteger cellIdentifierIndex = indexPath.item;
// Make one identifier the default cell for edge cases (we use CellType1 here):
if (cellIdentifierIndex >= cellIdentifiers.count) cellIdentifierIndex = 0;
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifiers[cellIdentifierIndex] forIndexPath:indexPath];
// Configure the cell …
return cell;
}
I've created a demo app to show a full implementation: https://github.com/widescape/StaticCollectionViewCells

How to create a 1 column tableview of checkboxes in cocoa

What are the correct steps to create a single column tableview with a fixed number of checkboxes. For example the column could have the following (X indicates the checkbox checked, _ indicates the checkbox unchecked. I also desire text headings in the same column.):
Heading one:
X item 1
_ item 2
X item 3
X item 4
Heading two:
X item 5
_ item 6
I prefer to do this using Cocoa bindings if possible. Also I need to know how to get the on/off checked states of any items that the user checks or unchecks. I need to know how to set the title text of the checkboxes to "item 1", "item 2" etc.
What I'm trying to do is use tableview to create a listbox of items with checkboxes like can be done in Microsoft MFC. Please be explicit in code explanations and describing IB steps as I'm very new to Cocoa and Objective-C.
Thanks.
OK, I have made this project for you and recorded in a screencast how I have made it.
Everything is so laggy just because it is too hard to screencast for my old 2008y MacBook
Project: Download
Screencast: Download
I'm writing from an iPad, so sorry for any mistakes, I can't test the code
1) in IB put a nstableview, make it 1 column in inspector, make it view based, name the column MainCoumn. Put checkbox in the tableviewcell.
2) select yor table view, open the forth tab in the inspector and connect delegate and datasource with AppDelegate.
3) in your AppDelegate.h add this:
<NSTableViewDelegate, NSTableViewDataSource>
After : NSObject
4) in your AppDelegate.m add this:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
if( [tableColumn.identifier isEqualToString:#"MainColumn"] )
{
NSArray *subviews = cellView.subviews;
NSButton *checkbox = [subviews objectAtIndex:1];
cellView.textField.stringValue = #"checkbox";
// [checkbox state]; - check is it checked
// [checkbox setState:0]; - 0 is to set it unchecked, 1- checked.
// if you need to make the second one checked, other - unchecked:
if (row == 1) {
[checkbox setState:1];
} else {
[checkbox setState:0];
}
return cellView;
}
return cellView;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return 6
// here you type the number of rows
}
5) profit111

Cell selection issue with EasyTableView

I have what must be a simple problem with EasyTableView (https://github.com/alekseyn/EasyTableView)
I have a number of horizontally scrolling tables that function properly.
I am able to select a cell and perform a segue, however, once the new view controller is dismissed, I am no longer able to select that cell and perform the same action until I have selected another cell in the same table.
My question is: How can I deselect previously selected the cell programmatically to renable this particular action.
Thanks in advance!
The selectedIndexPath is intentionally persistent in case a user scrolls the selected tableview cell offscreen and then back again. If you don't want this persistence please add the line shown below, after the delegate method (in EasyTableView.m):
- (void)setSelectedIndexPath:(NSIndexPath *)indexPath {
if (![_selectedIndexPath isEqual:indexPath]) {
NSIndexPath *oldIndexPath = [_selectedIndexPath copy];
_selectedIndexPath = indexPath;
UITableViewCell *deselectedCell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:oldIndexPath];
UITableViewCell *selectedCell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:_selectedIndexPath];
if ([delegate respondsToSelector:#selector(easyTableView:selectedView:atIndexPath:deselectedView:)]) {
UIView *selectedView = [selectedCell viewWithTag:CELL_CONTENT_TAG];
UIView *deselectedView = [deselectedCell viewWithTag:CELL_CONTENT_TAG];
[delegate easyTableView:self
selectedView:selectedView
atIndexPath:_selectedIndexPath
deselectedView:deselectedView];
// Add this line here!
_selectedIndexPath = nil;
}
}
}

Customize TableviewCell depending on section

How do I customize the background image of a UITableViewCell depending on the section, they are in? E.g. in section one, the cells are supposed to have a green .png image as a background image, whereas in section two, the cells are supposed to have a red .png image as background.
See Populating the Table View With Data for how to access and use section information.
When you have more then one section you implement some optional table view source methods to deal with sections.
Then when you create cell, check in which section it is and set it up accordingly, in:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath;
In this method you can access indexPath.section and format your cell.
See Table View Programming Guide for more.
UIKit adds property section to NSIndexPath so you can get section from index path passed in to the above method (see the first example I linked).
example:
if(section == 0) {
background.image = [UIImage imageNamed:#"green.png"];
}
else if(section == 1) {
background.image = [UIImage imageNamed:#"blue.png"];
}