A very simple question...
can we change the height of only current row(the row which is clicked) in UItableView?
Note: I dont want any affect on size of remaining rows.
Yes you can. You need a variable that keeps the last selected row. For ex.:
#property (nonatomic, assign) NSInteger selectedRow;
...
Then implement the method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == self.selectedRow) {
return 100.;
}
return 44.;
}
and then, update the method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(self.selectedRow == indexPath.row)
self.selectedRow = -1;
else
self.selectedRow = indexPath.row;
//The magic that will call height for row and animate the change in the height
[tableView beginUpdates];
[tableView endUpdates];
}
NOTE: Initialise your self.selectedRow with -1 value in the beginning as the default is 0.
You can use heightForRowAtIndexPath for setting height for a row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
You can place a check for NSIndexPath and return height for that particular indexpath
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == rowtochangeheight) {
return 60;
}
else
{
return 44;
}
}
Yes you can do, But you need to save indexpath of selected row and you must intialize selectedRow variable by negative value.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexpath.row;
[tblView reloadData];
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == selectedRow)
return 150.0f;
return 30;
}
Related
I am calculating height of Cell programatically in heightForRowAtIndexPath-
My complete method is:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
if([cell isKindOfClass:[Custom class]])
{
return 100;
}
else
{
return 20;
}
}
My app takes 3-5 sec to Show the ViewController. Although cellForRowAtIndexPath is called and I can show the logs in Console.
But My ViewController not loaded.
When just return the height using this code app works fine:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
I don't know that is the issue with this line:
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
What I am missing in this issue?
You have some logic in the cellForRowAtIndexPath: method that determines what type of cell that row should be. Add that logic into the heightForRowAtIndexPath: method to determine the what the height of each row should be.
Your this line is wrong
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
it will force your cell to reuse.
Use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell=[tableView cellForRowAtIndexPath:indexPath];
if([cell isKindOfClass:[CustomCellClass class]])
{
return 100;
}
else
{
return 20;
}
}
I have a custom UITableViewCell, and I it expands on selection. What I want to do is make the cells height go back to normal (44), if the cell that was selected was reselected. Here is my code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:self.selectedCell] && ![[tableView indexPathForSelectedRow] isEqual:indexPath]) {
return 100;
}
return 44;
}
The code works fine, but it seems to be ignoring the 2nd term in the if statement. Apparently I'm doing it wrong. Is there a way to fix it?
I don't know why you compare cell with indexPath.
[indexPath isEqual:self.selectedCell]
If you just want expand selected cell.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[tableView indexPathForSelectedRow] isEqual:indexPath])
{
return 100;
}
return 44;
}
UITableViewDelegate has deselect method
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
You need reload cell in this delegate
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
You need to store that particular index path in array to indicate that it is expanded Lets say you had selected 5 row that store that row number in an array, and reload that row after selecting it
Here is small code snippet that can help you to achieve this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if([_arrExpandedRows containsObject:indexPath.row]){
[_arrExpandedRows removeObject:indexPath.row];
}else{
// [_arrExpandedRows removeAllObjects]; // IF you only want to make it work for single row
[_arrExpandedRows addObject:indexPath.row];
}
[tableView reloadData];
}
And in your heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([_arrExpandedRows containsObject:indexPath.row]){
{
return 100;
}
return 44;
}
So what will it do if your array contains object of expanded row it will make reduce its row height and back to 44 and if not it will make it back to 100.
And above code will also work for multiple rows to expand and collapse at same time.
I think what you want is unselect cell when tap on a selected cell.
Store selectedIndexPath and compare in delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([selectedIndexPath isEqual:indexPath])
{
[tableView deselectRowAtIndexPath:indexPath animated:1];
}
else
{
selectedIndexPath = indexPath;
}
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
You can do something like this:
#interface ViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) UITableView* tableView;
#property (nonatomic, strong) NSMutableArray* selectedIndexPaths;
#end
<...>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:CELL_ID];
cell.textLabel.text = [NSString stringWithFormat:#"%i", (int)indexPath.row];
return cell;
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 44.0;
if ([_selectedIndexPaths containsObject:indexPath])
{
height = 100;
}
return height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//store or remove selected indexPaths
if ([_selectedIndexPaths containsObject:indexPath])
{
[_selectedIndexPaths removeObject:indexPath];
}
else
{
[_selectedIndexPaths addObject:indexPath];
}
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Starting my quest into ios development but I have a problem, below.
The plan:
The result:
You need to set height of your custom cell using tableview delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 150.0;
}else if (indexPath.row == 1){
return 300.0;
}
return 0.0;
}
as well as u need to set your custom cell cliptobound property to yes in datasource method
- (CGFloat)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customcell *cell = (customcell *)[tableView dequeResusableCellWithIdentifier:#"newcell" forIndexPath:indexPath];
cell.cliptobound = YES;
return cell;
}
Your tableview needs to know the height of your custom cell, use the following tableview delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 100.0;
}
return 200.0;
}
Your cells are getting reused without the content being reset in cellForRowAtIndexPath. In that method you need to blank out the cell content before putting more content in it.
I need to change dynamically the row of a cell. That's why I use selectedIndex variable to store the selected row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
TarModel * tar = self.tarList[indexPath.row];
if (selectedIndex == indexPath.row) {
static NSString *CellIdentifier = #"detailCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
else {
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
// Configure the cell...
cell.textLabel.text = tar.way;
cell.detailTextLabel.text = [NSString stringWithFormat:#"cost: € %#",
tar.price];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"indexpath is %d", indexPath.row);
selectedIndex = indexPath.row;
isSearching = YES;
[self.tableView reloadData];
}
and then I change the height of my selected row with this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
if(selectedIndex == -1) {
height = 44.0;
}
else if (selectedIndex == indexPath.row) {
height = 88.0;
}
else {
height = 44.0;
}
return height;
}
when I run this code and the first row is selected everything is ok, but if the second (only two rows in mytableview) is selected I get even all the next empty rows with the modified height. Where'I'm getting wrong?
for the update height of cell dynamically you can use below code :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 1)
selectedIndex = 150;
else
selectedIndex = 44;
[tbl_news_list reloadData];
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return selectedIndex;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 44;
}
else if (indexPath.row == 1) {
return 88;
}
else{
return 0;
}
}
Use reloadRowsAtIndexPaths inside didSelectRowAtIndexPath
reloadRowsAtIndexPaths will once again reload cells on didSelectRowAtIndexPath.
It worked fine , i expanded clicked row in demo app.:)
This should be easy, but I'm having trouble.
I have a static UITableView with a cell that I would like to remove programmatically if it's not needed.
I have a IBOutlet for it
IBOutlet UITableViewCell * cell15;
And I can remove it by calling
cell15.hidden = true;
This hides it, but leaves a blank space where the cell used to be and I can't get rid of it.
Perhaps a hack would be to change the height of it to 0?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:indexPath
{
//what would I put here?
}
Thanks so much!
You can't really deal with this in the datasource since with static tables you don't even implement the datasource methods. The height is the way to go.
Try this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell == cell15 && cell15ShouldBeHidden) //BOOL saying cell should be hidden
return 0.0;
else
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
Update
It appears that, under autolayout, this may not be the best solution. There is an alternative answer here which may help.
You can use tableView:willDisplayCell and tableView:heightForRowAtIndexPath with the cell identifier to show/hide static tableview cells, but yo must implement heightForRowAtIndexPath referring to super, not to self. These two methods work fine for me:
(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell.reuseIdentifier.description isEqualToString:#"cellCelda1"]) {
[cell setHidden:YES];
}
}
and
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if ([cell.reuseIdentifier.description isEqualToString:#"cellCelda1"]) {
return 0;
}
return cell.frame.size.height;
}
Depending on how your table is supposed to work, in your data source you can implement tableView:numberOfRowsInSection: to return 0 rows for the section based on your necessary logic.
Updated for your comment:
The section parameter will be populated by iOS when your implementation is called so all you need is a switch to handle the section that has the row you ant removed/hidden. Example below:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch(section) {
case 0: // first section of your table, change for your situation
return 0;
default:
return 0;
}
}
It for only constant cell
-(void)tableViewSearchPeopleCellHide:(BOOL)hide{
searchCellShouldBeHidden=hide;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
cell.hidden=hide;
self.searchPeople.hidden=hide;//UILabel
[self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (searchCellShouldBeHidden) //BOOL saying cell should be hidden
return 0.0;
else
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
The first thing you can do is tag the cell from storyboard which you want to hide.
Put some standard number which you can identify.
Add this code.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if (cell.tag==10) { //I have put 10 for some static cell.
cell.hidden=YES;
return 0;
}
cell.hidden = NO;
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
Set the cell you want to hide to hidden somewhere in your code. Add this code: (If your cell has different row height, then you need to override more functions)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowCount=0;
for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:section]; ++row){
NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
if (!cell.hidden){
++rowCount;
}
}
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int realRow=-1;
for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:indexPath.section]; ++row){
NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:indexPath.section];
UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
if (!cell.hidden){
++realRow;
}
if (realRow==indexPath.row)
return cell;
}
return nil;
}
Use index path to identify the cell in tableview height delegate and return 0
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if someCondition {
if indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 {
return 0
}
}else{
if indexPath.row == 4 {
return 0
}
}
return super.tableView(tableView, heightForRowAt: indexPath)
}