Different height for alternative cell in UITableView - iphone

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;
}
}

Related

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;
}

Height of cells in different tableViews in iOS

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;
}

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.

table view next results

How it is being implemented in table view? To limit the results display in the table view, I need to display eg 10 results, then at the last part of the table cell there's the "next results" part when selecting would load/insert the next set of data into the table view.
Pls. advise me. Thanks
You can make a 2 section UITableView...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (indexPath.section == 0)
return 10;
else {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableView *cell;
if (indexPath.section == 0)
cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"normalCell"];
else
cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"nextCell"];
if (!cell) {
if (indexPath.section == 0)
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"normalCell"];
else
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"nextCell"];
}
if (indexPath.section == 0) {
// Set your normal cells
} else {
// Set your next cell
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
// your normal cells here
} else {
// your next cell here
}
}
Of course this is not the only solution, but it works...