Change heightOfPrevious UITableViewCell - iphone

Is there anyway I can change the height of previous UITableViewCell when setting height for current cell in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath?
Have two rows in a UITableView. When in first row, it will set the default height.. When in row two I want to change the height of first row. Is this possible? If so how?
Thanks,
Bharathi.

Try to work with a boolean check if the requirement is fulfilled.
You could do it like this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0 && !secondRowIsSynced) {
return otherHeight ;
}
else {
return defaultHeight;
}
}

I think you misunderstand tableView:heightForRowAtIndexPath: is called multiple times (once for each row in your table).
If your rows have different heights then you need to determine which row you are on and what height it should have.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
CGFloat height;
if (0 == indexPath.row) {
// This is the first row
height = // what ever you want
} else if (1 == indexPath.row) {
// This is the second row
height = // what ever you want
}
return height;
}
If you later decide that a row needs to be a different height then it is still this method that needs to calculate the correct height to be used for each row. You can force this to be called without reloading the tableView like this
[tableView beginUpdates];
[tableView endUpdates];

bharathi:
You have to reload your table in the didSelectRowAtIndexPath method of UITableView.
You should check that the second row is selected, then reload all the table rows, so all tableview delegate and datasource methods get called again.

Related

Two UITableViewCell with different identifier and different height in storyboard

I have two type of cells that I'll be using my UITableView, so I created two prototype cell with different identifier. I manuelly changed the size but when I compile and run, the two cells have same size.
Is there any way to do it through storyboard and without checking every single time
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
I will have around a good 100 cells at least.
UPDATE: in my case I have a tableview with 3 sections, the first one is small, the second and the third one are bigger.
I think your best option is to use that -tableView:heightForRowAtIndexPath: delegate method you listed and return the height you would like.
You could assign tags to each prototype and use an if/else if conditional.
Or if you have a subclassed UITableViewCell for each prototype you could do something like
...
id cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[CustomCell1 class]]) {
// return aNumber;
} else if ([cell isKindOfClass:[CustomCell2 class]]) {
// return aNumber;
} else {
return aNumber;
}
...
If you know that the sections if what determines the height of the cell, then you can implement it like this:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath {
CGFloat result;
if (indexPath.section == 0) {
result = 160;
} else {
result = 80;
}
return result;
}
Depending on the heights that you need of course.

UITableview with variable row height crashes on reorder

There seems to be a UITableView bug, simple UITableView is created and the editing mode is set to be YES, all rows have varying height. For example,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0)
return 58;
else if(indexPath.row == 1)
return 520;
else
return 100;
}
Now on touching reorder control of second row application crashes.
Perhaps these row heights enforce the UITableView to auto scroll an invisible UITableViewCell from the bottom, but UITableView should have handled it.
Any idea….
You need to exchange it..not remove and re-insert it..
[self.itemArray exchangeObjectAtIndex:sourceIndex
withObjectAtIndex:targetIndex];
[self.tableView reloadData];

When click on button (button in cell) that particular cell should be changed?

There are 1 table on uiview and I want to change the cell height when button pressed other cell's height remain same
Pass the button press event to the view controller through delegate methods and reload the table view as follows.
[self.tableView reloadData];
In view controller (ie., datasource for the table view), implement heightForRowAtIndexPath method and return the height as required.
You can change the cell height in the delegate method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == clickedRow)
return newHeitht;
return cellHeight;
}
you can set some condition in button click and reload the tableview using [tableView reloadData]. this function will be called. return a new height for the particular cell.
-(void)buttonClick {
[self.tableview reloadData];
selectedRow = //do something here
}
and in your UITableview Datasource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
if(indexPath.row == selectedRow)
return selectedRowHeight;
else
return defaultHeight;
}
1) Create a custom UITableViewCell class.
2) Populate you cell however you see fit. I use a 'hydrateWithArray' type function myself.
3) Once the data has been populated, resize and reposition elements using 'sizeToFit' functions to force labels to conform to the size of whatever you put into them. protip: by setting the frame of the label first, and setting the numer of lines to 0... when you fill the text and sizetofit, it will stretch the label vertically only and force the width to stay the same.
4) Create a seperate function (mine is called calculatedHeight) that returns a float and returns the height that you would like the cell to be in the table (based on the repositioned objects from step 3).
- (float)calculatedHeight {
return textLabel.frame.origin.ytextLabel.frame.size.height5;
}
5) In your UITableView class, you'll need to import your tableViewCell class and create a dummy cell object. You're going to use this class to calculate how tall each cell needs to be. Then in the heightOfRowAtIndex method....
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height;
if ( !dummyCell ) dummyCell = [[CustomCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:#"myCell"];
[dummyCell hydrateWithTweet:[tableArray objectAtIndex:indexPath.row]];
height = [dummyCell calculatedHeight];
if ( height == 0 ) height = 50;
return height;
}
This is a pretty simple example so you may need to go crazy with the error checking in your particular use, but this should at least point you in the right direction. Enjoy!
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
return 110;
}
else {
return rowHeight;
}
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:toReloadRows withRowAnimation: UITableViewRowAnimationNone];
[self.tableView endUpdates];
then
[self.tableView beginUpdates];
[self.tableView endUpdates];

Increase the size of tableView Cell..?

Is it possible to increase the size of particular cell in the TableView.
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
return 100;
}
By using this we can increase the size of the table cell for a tableView but it will increase all the tableCell size.
But I want to increase the height of first cell is up-to 130 and other cell are of height 100. Can any one suggest me the best way for doing this...
Make height value dependent on cell's position (indexPath), e.g. to make 1st cell higher then others do the following:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath{
if (indexPath.row == 0)
return 130.0f;
return 100.0f;
}
*Note that with that code if you have several section then 1st cells in all sections will be higher then others.

tableView cell height... how to customize it?

Is it possible to modify height of only one cell in a grouped table view?
I have a table view with 2 sections of 3 and 2 rows... I would change row height of the second row of the second section...
How can I do this?
Thanks!
You can look at this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
In your case, the code should look like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1 && indexPath.row == 1) {
return SPECIAL_HEIGHT;
}
return NORMAL_HEIGHT;
}
You can look for more details about the method here
In iOS 8 and above we can use the Dynamic Table View Cell Height.
Using this feature UITableviewCell get its height from its content and We don't need to write heightForRowAtIndexPath
All I have to do in viewDidLoad()
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
If cell contains uilabel. then apply constraints to uilabel
Make sure you change Label --Lines-- to 0
The cell will grow automatically with content of uilabel:
You can implement the following method to return the height for the row at a given index path:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html%23//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
Have a look at
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
For the indexpath just check for which row and section and adjust the height accordingly