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.:)
Related
I have implemented a Dynamic TableView, Textfield and Buttons. My problem is, when I hide the button in the first row of my UITableViewCell, the other five rows of cell button also get hidden.
Can any one suggest a solution for this issue?
I have tried below code..
ladiesdetails=[[NSMutableArray alloc]initWithObjects:#"2",#"0",#"0",#"0",#"0",#"0", nil];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[passengerdetailcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([[ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.malebutton.hidden=yes;
}
return cell;
}
Just put the else condition and make the button visible in cellForRowAtIndexPath method. If you have any other condition to show add that as well.
if([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2) {
cell.malebutton.hidden = YES;
} else {
cell.malebutton.hidden = NO;
}
like this
bool flag = ([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2)
cell.malebutton.hidden = flag
It happens because of cell reuse , you use allocate the cell like below one .
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PassengerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PassengerDetailCell" forIndexPath:indexPath];
if([[self.ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.maleButton.hidden = TRUE;
}
return cell;
}
I want UITableView always has the text "No more items" in the last cell, I want this row appears regardless of number of items, include 0 items. How could I do?
You can get the total row of a section by using numberOfRowsInSection: method.
NSInteger total = [self.tableView numberOfRowsInSection:indexPath.section];
// Check if last cell or no cell.
if(indexPath.row == totalRow -1 || indexPath.row == 0)
{
// Do your operation here
}
You can perform this in your cellForRowAtIndexPath or willDisplayCell methods. Hope it helps.
Just add the row regardless of your other items:
- (void)buildMenu {
[_menuItems removeAllObjects];
// ... add your items or not if you have 0
YourCellItem *lastItem = [[YourCellItem alloc] initWithTitle:#"No more items"];
[_menuItems addObject:lastItem];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"yourCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
YourTableViewCell *basicCell = (YourTableViewCell *)cell;
YourCellItem *item = [_menuItems objectAtIndex:indexPath.row];
basicCell.itemTitle.text = item.title;
return cell;
}
I am using this in my app and i am create two identifiers for UITableview one is "normal cell" and another one is "expended cell" which will show when someone tap on a "normal cell". my problem is this that I can not manage heights of both cells separately for example I want normal cell height to be 120 and extended cell is to be 60. But if I change height in heightForRowAtIndexPath then both cells showing same height. here is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FSParallaxTableViewCell *cell = nil;
static NSString *cellIdentifier = nil;
if ([indexPath isEqual:self.expandedIndexPath]) {
cellIdentifier = #"ExpandedCell";
}
else {
cellIdentifier = #"NormalCell";
}
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[FSParallaxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if ([[cell reuseIdentifier] isEqualToString:#"ExpandedCell"]) {
cell.contentView.backgroundColor = [UIColor grayColor];
}
if ([[cell reuseIdentifier] isEqualToString:#"NormalCell"]) {
[cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]]
placeholderImage:[UIImage imageNamed:#"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
cell.clipsToBounds = YES;
}
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// disable touch on expanded cell
UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPath];
if ([[cell reuseIdentifier] isEqualToString:#"ExpandedCell"]) {
return;
}
// deselect row
[tableView deselectRowAtIndexPath:indexPath
animated:NO];
// get the actual index path
indexPath = [self actualIndexPathForTappedIndexPath:indexPath];
// save the expanded cell to delete it later
NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;
// same row tapped twice - get rid of the expanded cell
if ([indexPath isEqual:self.expandingIndexPath]) {
self.expandingIndexPath = nil;
self.expandedIndexPath = nil;
}
// add the expanded cell
else {
self.expandingIndexPath = indexPath;
self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
inSection:[indexPath section]];
}
[tableView beginUpdates];
if (theExpandedIndexPath) {
[theTableView deleteRowsAtIndexPaths:#[theExpandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
if (self.expandedIndexPath) {
[theTableView insertRowsAtIndexPaths:#[self.expandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
[tableView endUpdates];
// scroll to the expanded cell
[self.theTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
can anyone please help me resolving this issue I want to set height for both cells separately.
add below code in heightForAtIndexpath method
if ([indexPath isEqual:self.expandedIndexPath]) {
return 60.0;
}
else
return 120.0f;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
}
if ([[cell reuseIdentifier] isEqualToString:#"ExpandedCell"]) {
return 60;
}
if ([[cell reuseIdentifier] isEqualToString:#"NormalCell"]) {
return 120;
}
}
You have to set the height of table view cell separately. you can set the height separately according as the row.In following code i have set the 1st row height 35, 2nd row height 70 and rest of the row will have height 65.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case 0:
return 35;
break;
case 1:
return 70;
break;
default:
return 65;
break;
}
}
I am making a UITableViewController that resizes UITableViewCell when I select the cell.
My problem is that the content that should be hidden when the cell is not expanded (UILabel), is always visible.
What am I doing wrong? Here is my code:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"TableViewCell";
TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil] objectAtIndex:0];
}
cell.label.text = #"cenas";
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.selectedCellIndexPath != nil && indexPath.row == self.selectedCellIndexPath.row) {
return 200;
}else{
return 44;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.selectedCellIndexPath != nil && self.selectedCellIndexPath.row == indexPath.row) {
self.selectedCellIndexPath = nil;
}else{
self.selectedCellIndexPath = indexPath;
}
[tableView beginUpdates];
[tableView endUpdates];
}
And here are some pictures of what is happening:
In cellForRowAtIndexPath put the following code
if (self.selectedCellIndexPath != nil && indexPath.row == self.selectedCellIndexPath.row)
{
[YourLabel setHidden:NO];
}
else
{
[YourLabel setHidden:YES];
}
I found! I finally found!
I inserted inside of cellForRowAtIndexPath: the following:
[cell setClipsToBounds:YES];
This makes the content be only inside the cell
You need to manually hide/show the labels (Preferably with animation). Just because they are not inside the cells height, it doesn't mean they are hidden.
-(void)setSelectedCellIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *oldCell = [self.tableView cellForRowAtIndexPath:_selectedIndexPath];
//oldCell can be nil due to possibility that it's not on screen.
[self hideOutletsForCell:oldCell];
_selectedIndexPath = indexPath;
UITableViewCell *newCell = [self.tableView cellForRowAtIndexPath:_selectedIndexPath];
[self showOutletsForCell:newCell];
}
Also, you need to reset the outlets in cellForRowAtIndexPath, and only show outlets for the indexPath that is your _selectedIndexPath
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;
}