How to make UITableView footer for section not sticky [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
TableView Footer is scrolling with the table
I want to have a footer for each section that will not be sticky and would scroll with the table
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
This method defines a view for the footer that is "sticky".
Thank you in advance

Assuming you have a means by which to know when you're at the end of the data set for a particular section, why don't you just tag on a custom UITableViewCell that appears as the section footer you want? It's very simple to write the logic within tableView:cellForRowAtIndexPath: that would check if the cell being requested is within the data set for that particular section. As soon as the indexPath.row being requested is 1 greater than the available data (generally contained in an array, so if indexPath.row is equal to [array count]), return your "footer" cell. This will scroll along with the table as it scrolls, since it's just another cell. You can make it look as differently from the regular cells as you'd like.
Additionally, you would need to tell your table view that each section will have one extra row, so if you're saying something like
return [array count];
You would need to say, instead,
return [array count] + 1;
in your numberOfRowsInSection method.

Related

How to make dynamically adding of rows when user reached to the last row of the UITableView?

I have a UITableview which shows 10 rows currently which is fixed static. Now I want to add a feature into it. I want to add a more 10 rows to the table when user reach to the last row of the UITableView. I mean currently I am showing fixed 10 rows in the application.
But now I want to add 10 more rows whenever user reaches to the last row of previous UITableview.
Please give me any suggestions or any sample code to achieve my motive. Thanks in advance.
It is actually quite simple. What you need to do is implement the tableView:willDisplayCell:forRowAtIndexPath: method, which belongs to the UITableViewDelegate protocol. This method hits every time a cell is about to be displayed. So, it will let you know when the last cell is about to be displayed. Then you could do something like-
– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == [self.array count] - 1) //self.array is the array of items you are displaying
{
//If it is the last cell, Add items to your array here & update the table view
}
}
Another (a bit mathematical) option is to implement UIScrollView delegate methods (UITableView is a subclass of UIScrollView), namely scrollViewDidEndScrollingAnimation: or scrollViewDidScroll:. These will let you know the y-position of the content the user is viewing. If it is found that the bottom most content is visible, you can add more items.
HTH,
Akshay
uitableview is derived from uiscrollview. To achieve your objective, you need to implement scrollViewDidEndDecelerating:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
if (endScrolling >= scrollView.contentSize.height)
{
// your code goes here
}
}
This will detect a "bouncing effect" like shifting up the visible rows to indicate that one would like to see more.
How would you exactly want to invoke the loading of the additional 10 rows? When a user just scrolls down to see the first 10 that are loaded by default he might not want to load 10 more already.
You may add a "add more" line as the last row of your table. When the user clicks on this one you add 10 more.
(I don't know how one would detect a "bouncing effect" like shifting up the visible rows to indicate that one would like to see more.)
The basic logic would look like this:
in cellForRowAtIndexPath you check if the user clicked on the last line and then invoke your code to add the 10
to actually add 10 more lines you have to call [myTable reloadData]
but before you call you need to increase the returned value of numberOfRowsInSection by 10 and make sure that cellForRowAtIndexPath will correctly return your new lines 11-20
ps if you REALLY want 10 additional rows to be loaded when the user reaches the end of the table you need to invoke the loading of 10 more in cellForRowAtIndexPath or willDisplayCell:forRowAtIndexPath: when it is called for your last line.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [categoriesList count];
}
Here categoriesList is an array. We can add objects to this array and call reloadData in the tableview.

How to hide this part of uitableview

I designed a uitable view like in the image below , I want to hide the part under the second section , which I write on it (<- I want to remove this part ->)
any suggestion to do that
If you coppied the code from someone, this element is the fotter view for the second section. Look for the method:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
end erase it. It should be out.
You can try creating the table with specific height in the Interface Builder or:
UITableView* pTable = [[UITableView alloc] initWithFrame:(CGRect)];
Maybe you can also specify that scrolling is forbidden in order to show the contents that you want:
pTable.scrollEnabled = NO;
You might want to implement tableView:heightForFooterInSection: and tableView:heightForRowAtIndexPath: and return heights for the footer of the current section and header of the next section to reduce the gap between the two sections. This of course, if there are sections are the current section. If there are no more sections, then you can just reduce the frame size.

Delete tableview section header view

I have a tableview with 3 sections / 3 customs section header view...
After I delete a row in one of this section and if the section come empty, how I can delete the section header view.
Thanks
One of the approaches could be to maintain flags as to whether the section has become empty and return zero height for that section's header and footer and then execute reloadSections:withRowAnimation: when the only row in section is deleted.
This is certainly better when you have section specific customizations which would become tricky to handle if we were to remove the section from our model. If there are no customizations as such, you could go about maintaining an array of arrays. Once the row array is emptied, you can discard the section from the sections array. This will reflect on reloadData.
In either case, you will need to affect the model to change the view.
what you can do after you delete the row is call the method -(void)reloadData. If you are already doing this, you need to update your datasource to indicate that the row has been deleted and in - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you need to check that. Let me know if that helps!
One way I have dealt with this problem is implementing the section title like such:
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return ([[allData objectForKey:[keys objectAtIndex:section]] count] > 0) ? [keys objectAtIndex:section]:#"";
}
This just checks that the array of objects for that section actually has something if it does it returns the name of the section if not it returns an empty string which hides the header title.
The table view does need to be updated for the change to come into effect, also I was a dictionary full of arrays and an array full of the dictionary keys. but something similar could be easily implemented. This method allows for insertion back into that section without having to re-instantiate an array.
Hope it helps.

Add blank cell at end of UITable

I'm trying to add an additional cell for custom content as the last cell in a tableView, without altering the dictionary that creates the other content of the table. I think the place to add it is in cellForRowAtIndexPath rather than adding one to the numberOfRowsInSection, which just crashes if I do.
I get a cellCount from the dictionary that creates the data for the table, then in cellForRowAtIndexPath, I have:
if (indexPath.row == cellCount) {
... stuff goes here
return cell;
}
of course, this never gets called. If I do if (indexPath.row == cellCount -1) it overwrites the last cell with content.
I can get this to work if I add a blank entry into the xml from which I am populating the dictionary, but that's ugly.
Example code would be neat!
The problem here is that tableviews are designed to easily and accurately display the contents of you data-model and you've decided you don't want to do that. You're fighting the API.
The straight forward way to do this would be to put a check in numberOfRowsInSection such that it adds one to the row count when you want to display the input row. Then in cellForRowAtIndexPath: you will need to check if the table view is asking for the input row and return the appropriate type of cell.
From a UI design point, you might want to consider whether this setup is the best. This isn't a standard setup. Is the user going to understand that they can only edit the last row of the table? Will they understand they can't edit the other rows? Does anything in the UI tell them how all this works? Does the user have to scroll to the end of the table every time they want to add data? How long can this table grow? How will displaying a keyboard for the last row of the table affect how table scrolls?
I think it would be a better design to use a footer view to display the text field such that is is visually distinct from the rest of the table. It would be programmatically simpler as well. You wouldn't have to check if the table was asking for the last row every single time it ask for a cell.
Edit:
In thinking about it, perhaps a sectioned table would be simpler. Just put the special row in its own section (with or without a header.) That would simplify you handling of the rows that source from the dictionary because the row count in that section would always be the count of the dictionary. Likewise, you could just check the section attribute of the indexpath to know what cell to return for what row.
First you need to modify the numberOfRowsInSection to return +1. Then in cellForRowAtIndexPath you need to add that extra blank cell.
You need to provide for the extra cell in both cellForRowAtIndexPath and numberOfRowsInSection.
Assuming that cellCount is the actual number of cells in your array then: (a) in cellForRowAtIndexPath return the extra custom cell when indexPath.row == cellCount, and (b) in numberOfRowsInSection you need to return cellCount+1.
Assuming a single section, an example would go something like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [journalArray count] + 1; // add 1 for summary
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < [journalArray count]) {
return [journalArray objectAtIndex:indexPath.row];
} else {
return summaryCell;
}
}

Indexpath.row value not updating

in iphone application.
I'm trying to get indexPath.row value (out of didselectedrowatindexpath method) to do something on the basis of row selected in programmatically created tableview.
i need to access indexpath.row out of didselectedrowatindexpath method where if/else will define the action on the basis of indexpath.row.
there are 7 cards images in application and one [menu list]table view. whenever user will click on row of table view,then need to touch the image
I'm trying this code to get the IndexPath.row value. The problem is indexPath.row value is not updating everytime. It's just taking the old value. Please sugggest how to solve this issue.
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
NSUInteger nSection =[myTableView numberOfSections]-1 ;
NSUInteger nRow = [myTableView numberOfRowsInSection:nSection];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:nRow inSection:nSection];
NSLog(#"No of sections in my table view %d",nSection);
NSLog(#"No of rows in my table view %d",nRow);
NSLog(#"Value of selected indexPath Row %d", [indexPath.row]);
NSLog(#"VAlue of Array arrOperationChk %d",[arrOperationChk count]);
}
This code appears to respond to something (the table?) being touched. You then ask the table how many rows it has in its last section and create an indexpath to that.
The table caches the number of rows in each section. If you have changed the number of rows, you need to tell the table, either by calling -insert/deleteRowsAtIndexPaths:withRowAnimation:, or by calling -reloadData. Otherwise the table has no way to know that it needs to re-query its datasource (which you provide).
Unless I'm reading this code wrong, aren't you just getting the index path to the last cell+1 of the last section? I wouldn't expect that to change.
If you want to get the selected cell, use the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method in your UITableViewController object.
As other people have said, there's nothing in your code that would change the indexPath variable you've just created.
Also, the syntax [indexPath.row] looks wrong - you don't need the square brackets there unless you're calling a method. When you use the dot syntax like that on a pointer in Objective-C, you don't think of it as a method call (even though there is one, implicitly), but rather as a pseudo-instance variable as of a struct.
What is your big picture goal? If we understood what you are trying to achieve / what is the desired behavior, maybe a more useful answer will arise.