iOS- How do I make each cell in a table view bigger? - ios

This might be a noob question,but how do I make each cell in a UItableview bigger through interface builder?

Select the table view in your nib or storyboard. Then show the Size Inspector. (You can do this by choosing View > Utilities > Show Size Inspector… from the menu bar, among other ways.) The first section of the Size Inspector is the Table View Size, and the first field in the Table View Size section is the Row Height.
Changing the Row Height field in the nib has the same effect as setting the rowHeight property of the table view instance at runtime.
If you want rows to be different heights, you must either implement tableView:heightForRowAtIndexPath: in your table view's delegate, or use a storyboard with static cells.

Assuming you have set the dataSource and delegate to self, this should work:
- (CGFloat)tableView:(UITableView *)tableView heightForCellAtIndex:(NSIndexPath *)path
{
return 50.0f; //Replace with how high you want.
}
Typed on mobile, test before shipping.
Yes, it's code, but it's a whole lot easier than anything else. Easier to change, too.

Select the Table View Cell, then choose the ruler icon on the right panel, check Custom next to Row Hight, then just change the row height.
To change the width select the view controller, find Simulated Metrics in the tab to the left of the ruler, change Size to Freeform, then select the Table View, go back to the ruler tab and you should be able to change the width which the cell will adjust to if Autoresize Subviews is checked (which it is by default).

Related

How to edit a table view cell's text in Interface Builder?

I am trying to design a settings screen. I added a table vc in my storyboard and changed it to static cells. Then I configured the number of sections and the number of cells in each section.
Now I want to add text to each of the table view cells. I tried to select the cell and edit the text property in the property inspector. But I could not find a text property. I then tried to drag a label to the cells' content views. But I can't put the label in exactly the same place as the others. Also this method is very tedious.
I mean there is even a property called textLabel in UITableViewCell class! How can there not be a corresponding property in the interface builder?
How can I do this in IB? Must I use code to set the text of each cell?
Make sure you selected Style: Basic at the Cell's Attribute inspector, and then select the label.
Also, because of the cell has a contentView, the easiest way is to find it in the view hierarchy, or click on the cell until you see the inspector of the label

How to set the height of prototype cells in an iOS table views

I have a table view with a prototype cell containing both disclosure and detail accessories. I want to use a font size of 12 in the cell and reduce the row height accordingly. Thus the size of the two accessory icons need to be reduced in size as well. I assumed this would be one of the more obvious things one might want to do with a table view but fail miserably in figuring out how?
Assuming you are have a nib or storyboard, you should see a resize handle on the cells when you select a cell in the editor. Just grab and resize.

What is good practice for code to dynamically add content to View Controller?

I essentially want to have the following scenario.
Very simply, each entry contains a number label, two header labels and two text fields.
Being new to iOS development, I'm not sure of an intelligent, simple way to do this. What is the best way to go about doing this?
This is a great case for using a UITableView. Create a table and a cell prototype that has all the elements that you need (a number, fields for name and age, etc). Create also a different cell prototype for "add more". That would be the last cell in your table. Increment the number of cells when a new record gets added.
There are multiple tutorials online for UITableView. What you want to look for is how to set up and use 2 different cell types. See, for example, this SO question.
If you go with a table view as opposed to just adding some subviews, you get some useful features for free:
scrolling, if you have a lot of records
animated insertion of rows
support for deleting rows/records
easy customization of your list of records: headers, footers, sections.
You can do this manually - there is no problem. Create UITextField, UILabel etc objects in the code, configure them to your liking and add them using
[view addSubview: ]
method
I would:
Create a scroll view - UIScrollView, set the size of this scroll to the size of your main view and add this scroll view to your main view.
Create a content view - UIView, set the width of this content view to the with your the scroll view. But the height should be 0. Set the scroll view in step one's setContentSize: to content view's size. Add this content view as a subview of the scroll view.
Create an iVar to keep track of how many buttons set has been used.
Now every time the + button tapped, create buttons, labels, etc., use the iVar in 3 to calculate and adjust the frame for each buttons, labels, etc.
Adjust the content View's frame to make room for the new set of buttons, labels, etc. Update the scroll view's contentSize: to the content view's frame size.
Add those buttons, labels, etc. in step 4 to content View as subviews.
Case A:
If you are going to add limited number of this type of view
Create one viewController, add textField, label etc to view of this view controller.
Create new instance of this viewController when user clicks add button then use addSubview by refering previously added view's y position
Case B : If number is not limited you will need scroll, then tableView is best option
You will need to create custom cell with your labels and textfield
OR
you can do same using prototype cell if you are using storyboard.
When user will click add button do this
//Update your datasoure for new cell
[tableView insertRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationTop];
You can create add more view and set it as table footer as it will always stay at bottom, like this
table.tableFooterView = assignYourAddMoreViewHere;

Grouped UITableView - remove 'Prototype Cells' header in interface builder

I have a UITableView placed on my view controller in IB, using 2 prototype cells and 'Grouped' style. There will only ever be 2 cells in the table, and no group header so I want to resize the height of the table view control to be just tall enough to accomodate the 2 rows (each of height 29) so I have room for my other controls on the VC. Problem is, when I resize the height of the table in IB, it always shrinks at the bottom and retains the 'Prototype Cells' label. This means to get the right height and position I have to lose visibility of my cell designs - see screenshot :
Is there anyway to turn off the 'Prototype Cells' group header label so I can resize my controls WYSIWYG style?
It will be more useful to show your code and what UITableView delegate methods you have implemented in your VC. Anyways, try this : set your header #property for the tableView to nil and if you get no result implement this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return nil;
}
Hope this will work. Cheers
It is possible to change the uitableview to static in stead of dynamic cells. This allows you to add the cells in the layout editor. You can then make iboutlets to the cells and there content. The downside of this is that you will have to use a UITableViewController as root which in turn doesn't let you add anything else then cells, headers and footers.
You can still add the rest of your components in a separate sections and just set the cell to background to invisible. or you could load the controller grab the view and resize it and add it to another viewcontrollers view.

Static table view cell's height in xib not being applied

I have created a static table cell in a .xib, however when it is displayed the cell height set in the xib is not being applied, instead the cell is being displayed with what looks like the default height for cells.
I have set things up as:
1) The table view controller derives from UITableViewController
2) The TVC is not contained within a xib and is created in code in the app delegate and added to a tab bar controller.
3) The TVC is created using initWithNibName:bundle: with the nib name supplied as the name of the xib containing the table view and the static table cell.
4) The TVC has an IBOutlet iVar of type UITableViewCell*
5) cellForRowAtIndexPath returns this iVar.
6) numberOfRowsInSection returns 1, numberOfSectionsInTableView returns 1;
7) The xib contains a table view, which is connected to the File's Owner's view. The File's Owner is set to my custom table view controller class.
8) The UITableViewCell in the xib is connected to the outlet of type UITableViewCell in the File's Owner.
9) THe Table view style in the xib is set to grouped.
10) The size of the Table View Cell shown in the size inspector is N, and this is where the problem is. If I change N manually or by resizing the cell visual representation then this size is not applied when the table is drawn. I have also tried setting the view Frame Rectangle height in the size inspector to match.
It doesn't matter what value N is, big or small.
11) The cell's height is not being set anywhere in the TVC, if I do set it using self.tableView.rowHeight then its height does change. But I do not want to set it explicitly like this, I want the height size in the xib to be picked up.
This has been driving me nuts for a few days, and reading several tutorials on table views (including Apple's table view programming guide, especially the section on static cells, whose instructions I followed in the first place to set everything up) has not given me any clues where the problem lies.
TIA
Implement heightForRowAtIndexPath and return the height of the UITableViewCell linked to in your IBOutlet.
Do you have - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath defined in your UITableView delegate?
You may want something like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30;
}
Try this post if you're still having trouble try this post: Setting custom UITableViewCells height
I had the same problem and everyone here and on other threads suggests writing code to fix the problem, which seemed wrong. What's the point of using IB if you have to write code to get the properties right?
The answer turns out to be very simple but took a bit to track down. In IB there are two places that row height can be set (just like there are two places to set it in code). One is on the table, the other is on the cell.
The place I was (incorrectly) setting it was the tableViewCell's "Row Height" property in the Property Inspector. Changing this value had no effect on row height at runtime.
Then I found and set the "Row Height" property of the TableView (one step up the object hierarchy) and viola, the row height is correct at runtime. Once this was working I turned off the override of Row Height on the TableViewCell (uncheck "Custom" on the Row Height property). I was afraid this might be evaluated on each cell, which would kill performance since all my cells are the same height.

Resources