Accessing "default" methods of UITableView - iphone

I try to set up a tableView. I use standard cells for all sections' rows except in the last section (containing one row). Thus, I would also like to use the standard layout for all those sections except that special one.
A short example is the following, my "special" cell is in section 3 (there is only one row):
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 3)
return 5;
return **????**;
}
At ??? I would like to return the width calculated from UITableView (just as if I did not implement the method).
[super self:tableView
heightForHeaderInSection:(NSInteger)section];
does not work. I know I can access
[tableView setionHeaderHeight]
which is by default 10 and obviously does not take into account that I have section headings for the other sections, which will require additional space. I tried that, but it will then get the sections too close (see screenshot):
(Note: the section I am interested in is the one which does not look like a cell: the one with the dates (invisible background)).
So, the easiest thing would be to hand over the layout to the standard implementation which is perfect - except for section3.
What are my options?

Just in case: there is a new constant introduced in iOS 5, called UITableViewAutomaticDimension. As the documentation says, you should return it from your delegate method when you want UITableView to use a default value.
So, the code for your case would be:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 3) {
return 5;
} else {
return UITableViewAutomaticDimension;
}
}

You seem a bit confused about heightForHeaderInSection - it returns the height of a table section header (this is the "title" of a table section), not a row. iOS calls this method to ask for the height of just a single section header, irrespective of any other section headers there might be.
If you want to use the default, just return [tableView sectionHeaderHeight] for any section other than 3 - you don't need to "take into account that [you] have other section headers", as it's asking for the height of the header for section alone. It will ask again for the heights of others (and compute the relative positions with of rows and other sections automatically).

You do not have a super implementation tableView:heightForHeaderInSection: since you are not subclassing any abstract base implementation for UITableViewDelegate. The table view is instead decided if the default height should be used by inspecting your delegate implementation to see if the method is available.
It is a quite a huge concept to wrap your head around, especially if coming from Java or C#. Methods in Objective-C protocols can be optional, and their absence means use default.
Your method should probably be implemented as:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 3) {
return 5;
} else {
return 36;
}
}
The default height for grouped and plain tableviews are different (22points for plain). The default values are not exposed by UITableView, not even as private methods. File bug at http://bugreport.apple.com to make this a public constant.

After overriding heightForHeaderInSection and doing a side-by-side comparison, the height for the header in the first row is larger than the rest. This isn't pixel perfect, but it's very close:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 ) {
return 46.0;
} else if (section == myCustomRow) {
return 12345.0; // custom height
} else {
return 36.0;
}
}

Related

UITableView fixed header

How to disable scrolling for UITableView header, to be always visible? (To have column titles.)
I am not sure if I am getting what you mean, are you using a UITableViewController?
If you want to have a header that is not scrollable maybe you can try to put a UIView at the top of your tableview and add sublayers to it as needed.
If a tableview with multiple fixed section headers is what you want, I would suggest using different tableviews and adding UIViews on top of each one for the header.
For handling delegate and datasource use NSObject's - (BOOL)isEqual:(id)anObject .
For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([tableView isEqual:markosFirstTableView])
{
return 1;
}
else
{
return 5;
}
}
Does this help?

iPhone UI Design question - Best way to design forms?

I want to design an app that needs user to input few things like start date, end date, bunch of other options and some text comments for which I am planning to use pickers to select the data that will slide up modally. I will need to move the view up and down to make sure that the element being filled stays in focus when the pickers and keyboard slides up and down.
My question is what would be the best view to implement such a "form"? I was thinking grouped table view where I could separate the fields section wise.
Is there any other way to implement these things?
By experience or best practices, are there any better alternatives or sample code or apps out there that I can explore?
Dev.
The most iPhone-like interface for forms is going to be a grouped table view. It is what most users will expect, after using other apps which use grouped table views for adding and editing structured data.
A good practice is to create an enum (enumeration) for sections and for rows within sections, e.g.:
typedef enum {
kFormSectionFirstSection = 0,
kFormSectionSecondSection,
kFormSectionThirdSection,
kFormSections
} FormSection;
typedef enum {
kFormFirstSectionFirstRow = 0,
kFormFirstSectionSecondRow,
kFormFirstSectionRows
} FormFirstSectionRow;
...
In this example, you can use this enumeration to refer to sections by name instead of number.
(In practice, you probably wouldn't use kFormSectionFirstSection as a descriptive name, but something like kFormSectionNameFieldSection or kFormSectionAddressFieldSection etc., but this should hopefully illustrate the structure of the enum.)
How would you use this?
Here's an example of a few table view delegate methods which demonstrate how this is useful:
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return kFormSections;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case kFormSectionFirstSection:
return kFormFirstSectionRows;
case kFormSectionSectionSection:
return kFormSecondSectionRows;
...
default:
break;
}
return -1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// cell setup or dequeue...
switch (indexPath.section) {
case kFormSectionThirdSection: {
switch (indexPath.row) {
case kFormThirdSectionFourthRow: {
// do something special here with configuring
// the cell in the third section and fourth row...
break;
}
default:
break;
}
}
default:
break;
}
return cell;
}
This should quickly show the utility and power of enumerations.
Names in code are much easier to read than numbers. When you're dealing with delegate methods, if you have a good descriptive name for a section or a row, you can more easily read the logic of how the table view and cells are managed.
If you want to change the order of sections or row, all you have to do is rearrange the order of enumerated labels in the enum construct. You wouldn't need to go into all the delegate methods and change magic numbers, which quickly becomes a tricky and error-prone dance once you have more than a couple sections and rows.

How can a UITableViewCell know of its own indexPath?

The standard Grouped UITableView style allows UITableViewCells to be drawn with rounded corners at the top and bottom of each section. How is this accomplished? How does the cell know its own location within its section, and how does it know when to change its rounded edges?
I want to make my own rounded cells, and I have images to use, but don't know when to show which image
Note: I already know how the UITableView works, and I know how to use it. I just thought that since a UITableView is able to automatically draw rounded corners at the correct places, I should be able to as well, without needing to add anything to my data source or delegate.
NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self];
int rows = [(UITableView *)self.superview numberOfRowsInSection:indexPath.section];
if (indexPath.row == 0 && rows == 1) {
// the one and only cell in the section
}
else if (indexPath.row == 0) {
//top
}
else if (indexPath.row != rows - 1) {
//middle
}
else {
//bottom
}
It's very simple. suppose cell is the object, whose position is to be found out.
UITableView* table = (UITableView *)[cell superview];
NSIndexPath* pathOfTheCell = [table indexPathForCell:cell];
NSInteger sectionOfTheCell = [pathOfTheCell section];
NSInteger rowOfTheCell = [pathOfTheCell row];
There is sectionLocation method of UITableViewCell that returns integer telling you what you need:
1 - middle cell
2 - top cell
3 - bottom cell
4 - single cell
I had no issues using this in several production apps since 2010.
UPDATE: one of our binaries was automatically rejected recently (end of 2018) because we were using 'sectionLocation' property, so it's not a good option anymore.
Add something like this into your header files and you can use it:
typedef NS_ENUM(NSInteger, MMMTableViewCellLocation) {
MMMTableViewCellLocationUndefined = 0,
MMMTableViewCellLocationMiddle = 1,
MMMTableViewCellLocationTop = 2,
MMMTableViewCellLocationBottom = 3,
MMMTableViewCellLocationSingle = 4
};
#interface UITableViewCell ()
/** Undocumented method of UITableViewCell which allows to know where within section the cell is located,
* so the cell can draw its borders properly. */
- (MMMTableViewCellLocation)sectionLocation;
/** Override this one to know when the value of sectionLocation changes. */
- (void)setSectionLocation:(MMMTableViewCellLocation)sectionLocation animated:(BOOL)animated;
#end
You can use
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
for this issue. In my example I am using this to scroll the cell (with custom content) to the top of the view.
If you need more robust and general stuff, take a look at http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html - Matt Gallagher shows what you need, pretty effectively. He basically recreates UITableViewController from UIViewController, while adding ability to use your own custom graphics. I'm just working on applying this to one my projects, so far it looks it would do the job.
Unfortunately, I have found no solution to this problem, and have resorted to subclassing UITableViewController and UITableViewCell into a generic solution that I can extend as necessary.
You don't do this in cell. Rounded corners are drawn in [tableView viewForHeaderInSection] and viewForFooterInSection.
The way I do it is to use Plain tableview style, then use these two views for roundness and cells are normal, no rounds.
Without getting into who draws what, you can know which cell is the last cell in its section inside of cellForRowAtIndexPath very easily.
You're passed in the indexPath of the cell you need to provide, right? You're also passed the tableView.
call [tableView numberofRowsInSection:indexPath.section] and if it's == ([indexPath.row]-1) you know you're being asked to supply the last cell in that section.
At the time that cellForRowAtIndexPath is being called, the cell is guaranteed to be at the indexPath passed in.
To expand upon Darren's answer (which I found most useful, thanks Darren!), what you can do is to iterate through all of the superviews' until you find the parent UITableView. This should be future proof since you do not rely on a fixed hierarchy of views.
I use a recursive method that will return the UITableView if it finds one or return nil if there is none.
- (UITableView *)parentTableViewOf:(UIView *)view {
Class class = [view.superview class];
NSLog(#"Class : %#", NSStringFromClass(class));
if([view.superview isKindOfClass:[UITableView class]]) {
return (UITableView *)view.superview;
} else {
return [self parentTableViewOf:view.superview];
}
return nil;
}
So far I've used this one and it seems to work without hiccups. Hope it helps! :)
The cells dont know where they go...The table view has cells, You are the one telling the table view WHAT goes in the cell. You do this in the DataSource where you implement cellForRowAtIndexPath...The way this works :
An index path has a row and a section
For a grouped table view
A section pertains to a group, and a row pertains to 1 entry in that section,
the way UITableView knows how many rows are in a section and how many sections there are is the DataSources methods numberOfSectionInTableView and the method numberOfRowsInSection, this will make the right calls to cellForRowAtIndexPath, it is up to you to recognize which section and row is being queried and you need to build your cell according to these specifications.
A good way to do this i s you can have a Dictionary with keys of section names and values of NSArray with the values that go in that section.
So you implementation for numberOfSectionsInRows would look like
return [[dictionary allKeys] count]
And the implmentation of numberOfRowsInSection would look like
NSString* key=[[dictionary allKeys] objectAtIndex:sectionNumber]
return [[dictionary objectForKey:key] count]
You can always refer to the UITableView programming guide at http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/Introduction/Introduction.html
Hope that helps
Simply add a property to your custom UITableViewCell (depending on implementation) class that contains an int, NSNumber, or an NSIndexPath specifying which one it is. In you're using a data structure instead, then put it in you element in that data structure. Then you simply set the property when you create the data structure, something like elt.id=i, and then you access it in the cellForRowAtIndexPath, something like if (elt.id == 0 || elt.id == n-1) where n is the number of rows in your section.
I might have totally missed your question, but if I did, just comment and I'll post again.

Custom UITableView section header views are breaking

I have a custom UIView subclass that i'm trying to use as a header for one of my grouped tableview sections. I save an instance of that view in the tableViewController and use that to return the height for the header section as well as the view itself. the problem is that somehow that instance variable changes from a UIView to a CALayer in the middle of a reloadData call which causes a crash, since the instance has a special method to return it's expected height. this is the code that crashes:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return [self.dataHeader frameHeight];
}
return 0.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return self.dataHeader;
}
return nil;
}
I set a breakpoint at the first return in the if block of the heightForHeaderInSection method, and it hits it 4 times; the first three return the dataHeader successfully, while the fourth time shows it to be a CALayer and crashes with a doesNotRecognizeSelector exception (my tableview has 2 sections if that makes a difference). Is there any reason why this happens, and is there a way to stop it?
What does your initialization code for dataHeader look like? When you initialize dataHeader, are you properly retaining it?
My guess is that your dataHeader view is getting released before you intended.
The problem seems to be that you have 2 sections and somehow the app thinks there are 4. Here's how I would debug this problem:
1) What is numberOfSectionsInTableView returning (is it implemented)?
I assume that each header method should be called n times, where n is the number of sections in your table. I would also assume that the app asks the aforementioned delegate what n is.
2) What are the values of section each time these delegates are called?
There should only be one call per section, unless I'm missing something, and I would be amazed if the delegate gets called more than once with the same section value.

iPhone + UITableView + row height

I am setting the row height of my UITableView using following code
[tableView setRowHeight: 100.00];
I am using the single line as separator in the UITableView.
Eventhough setting the height above, height of row does not get change.
You should implement
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
delegate method. and return 100.0 there.
You should avoid the heightForRowAtIndexPath if all your rows are of similar height and use the rowHeight property. According to the documentation:
There are performance implications to using tableView:heightForRowAtIndexPath: instead of rowHeight. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).
In the UITableViewController subclass it could be done, for instance, in the viewDidAppear method (the UITableViewController has a reference to the tableView):
self.tableView.rowHeight = 79.f;
The row height is baked into the cells when they are first displayed.
Did you set UITableView#rowHeight before setting the data source?
If not, do so.
If for whatever reason you can't, your other option is to call UITableView#reloadData after setting the row height.
I did like this, here tableobj is nothing but UITableView object in my application.
- (void)viewDidLoad
{
[super viewDidLoad];
[tableObj setRowHeight:100.0f];
}
Or handle it in numberOfRowsInSection: like:
- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section {
[tableObj setRowHeight:100.0f];
return [soandso count]; // soandso is my object
}
Because set the rowHeight before setting the data source. It worked for me (for equal row heights).
The better and cleaner solution is to implement the delegate function (Maybe not the best one if your UITableView has many rows ...).
Also, think that UITableVieCell are UIView so you could change their height firstly...
Delegates are very powerful in iPhone dev, here the UITableViewDelage:
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
You can also change the indentForRow, displayCellForRow, heightForRow,edit stuff .....
I dont think there is such a method in UITableView...
Instead you can use the property rowHeight...
Try,
tableView.rowHeight =100;