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.
Related
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;
}
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;
}
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;
}
I have a UITableView put in edited mode.
self.tableView.editing = YES;
In this table view, I have some lines displayed in a custom cell, and I'd like to add one at the end that would allow the user to add an item (using another view).
So I wrote some lines :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return "number of lines" + 1;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row+1 != [tableView numberOfRowsInSection:0]) {
return UITableViewCellEditingStyleDelete;
}
else {
return UITableViewCellEditingStyleInsert;
}
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row + 1 == [tableView numberOfRowsInSection:0]) {
cell.textLabel.text = #"Add a line...";
}
else {
do the stuff in the custom cell
}
}
Doing this way, the UITableView allows to rearrange any line. I can move the first line after the "add line", and move the "add line" in first position.
How may I remove the arrange button on the "add line" cell and prevent other lines to go under this one ?
Or is there a better way to code this ?
Implement
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([indexPath row] != INDEX_OF_SPECIAL_ROW)
}
and
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if ([proposedDestinationIndexPath row] < NUMBER_OF_ROWS)
{
return proposedDestinationIndexPath;
}
NSIndexPath *otherPath = [NSIndexPath indexPathForRow:NUMBER_OF_ROWS-1 inSection:0];
return otherPath;
}
The much easier way would be to set a tableFooterView. You can place any UIView in there, so not only are you allowed to add a UITableViewCell but also a completely custom subclass. Doing it this way you will avoid all the unnecessary checks.
return UITableViewCellEditingStyleNone
For that row in - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
And check out – tableView:moveRowAtIndexPath:toIndexPath:
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;
}
}