Height of cells in different tableViews in iOS - iphone

In my view, i have three different tables. For first two tables, i have a fix height for each cell(e.g. say 30.0f for each cell of first table and 45.0f for each cell of second table) which i have adjusted in the storyboard according to the data in each table. For the third table, i want to have a different height for each cell. I tried the method
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
But the issue with this technique is that this method is called for each table. As for the first two tables, i have adjusted the height in storyboard, i don't know what to do with them.And if i do:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == myTable) {
return 150.0;
}
}
As i have nothing to return for the else cases, the whole thing fails.
Any suggestion?

You need to return a height for the other tables.
If you have set there height in the storyboard you can access that height like so:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == myTable)
return 150.0;
return tableView.rowHeight;
}
tableView.rowHeight will be the height that you set in the storyboard

You should make your view controller delegate of all 3 tableviews and in heightForRowAtIndexPath check which table called the method:
So in viewDidLoad add:
myTable1.delegate = self;
myTable2.delegate = self;
myTable3.delegate = self;
and then:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == myTable1) {
return 150.0;
}
else if(tableView == myTable2) {
return 75.0;
}
else if(tableView == myTable3) {
return 75.0;
}
}

How about using the tag property? Set the tag for each table to a unique number then return height like this:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (tableView.tag == 1)
{
return 150;
}
return 250;
}

Related

Give the header only to specified table

I had two UITableView in my project and I am giving custom header to one table using the method:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(tableView.tag == 3)
{
SectionInfo *array = [self.sectionInfoArray objectAtIndex:section];
if (!array.sectionView)
{
NSString *title = array.groupdeck.groupTitle;
array.sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 0, tblData.bounds.size.width, 45) WithTitle:title Section:section delegate:self];
}
return array.sectionView;
}
else{
return 0;
}
return 0;
}
It is giving the header to the table with the tag 3 like:
But it is giving the default header to other table also even return 0 else condition like:
What am I missing?
Try :
otherTable.sectionHeaderHeight = 0.0;
You don't have to do anything else.
Or:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(tableView.tag == 3)
{
//Required height.
}
else
{
return 0.0;
}
}
It might be defaulting to the default header because you returned a 0. Try returning nil instead.
From: tableView:viewForHeaderInSection: default value?
With Reference to written in Apple Documentation
// custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
It is displaying the default header which has height of 22 for grouped table and 10 for nongrouped table.
Also if the height of your view which you want to display in UITableView is more than above values then you also have to use the UITableView Delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

Increase height of cell in Application setting screen

I am using this Tutorial for developing application setting screen.The problem is i want to increase height of cell in setting screen.And I have not taken any table view i am doing that by using Root.plist file.i.e i want to increase height of Legal field in this Image (application setting screen not in table view of application)
First, you'll need to save the selected indexPath row:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedRowIndex = [indexPath retain];
[tableView beginUpdates];
[tableView endUpdates];
}
Then, when you have the currently selected index, you can tell the tableView that it should give that row more space.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//check if the index actually exists
if(selectedRowIndex && indexPath.row == selectedRowIndex.row)
{
return 100;
}
return 44;
}
This will return height 100 for the selected cell. You can also check this
Use this set row height in a UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Return the height For Row.
return 100.0;
}
As it can be seen from your image the indexPath for which you need to increase the row height is section 1 and row 0:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *indexValue = [NSIndexPath indexPathForRow:0 inSection:1];
if (indexValue == indexPath)
return 100.0;
else
return 44.0;
}

how can create more than one UITableView in a UIViewController programmatically?

I want to create 2 UITableView programmatically in a UIViewController. I have 2 NSMutableArray for the datas which are *firstTableDatas and *secondTableDatas. I can manage to create one UITableView programmatically but when it becomes 2 then I am confused what will be the return values for the 2 UITableView for the delegates:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
}
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
}
and how can i load the datas in 2 individual UITableView s.
Thanks in advance.
use
if(tableView ==Firsttable)
{
...
}
else
{
...
}
in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
...
}
You can check like below:
if(tableView ==firstTableDatas)
{
//Code related to First Table
}
else if(tableView ==secondTableDatas)
{
//Code related to Second Table
}
It will work in UITableViewDelegate methods.
Happy Coding.
give diff tag to both the tableView and on the bases of there tags do the required function
for example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView.tag==1)
{
return 100;
}
else if(tableView.tag==2)
{
return 50;
}
return 40;
}

How to set height of cell in grouped style table?

I have made a table with two types of custom cells using interface builder:
Everything works fine, except lower cell's height is same as cell above it. It has everything i put - UIImageView etc...
So I was thinking if it is not possible to change cell height when table style is grouped?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 1) {
return 100;
}
return 44;
}
We can use UITableview Delegate Methods :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1)
{
return 60;
}
else
{
return 44;
}
}
You have to use this
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75.0f;
}
it is use full for me.

Different height for alternative cell in UITableView

How to make a alternate cell with a different height?
I need height for cell1 is 60 and cell2 is 30....
how can i do this?
Thanks in advance.
you can set the height of cell from the delegate method of table view
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0)
{
return 60;
}
else if(indexPath.row==1)
{
return 20;
}
}
and so on....
Happy coding....
Use the modulus operator and you dont need an if case for every row index :)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0)
{
return 60;
}
else
{
return 30;
}
}