Using rounded-rect button for TableView footer in iPhone? - iphone

I'm trying to put a button to footer of TableView.
This is what i did and I can't see the button but the string "RESULT" is displayed.
I've tried everything I can think of. but I couldn't figure out what's wrong. I need you help!
- (void)viewDidLoad {
btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnSeeResult setTitle:#"RESULT" forState:UIControlStateNormal];
viewResult = [[UIView alloc] initWithFrame:btnSeeResult.frame];
[viewResult addSubview:btnSeeResult];
self.tableView.tableFooterView = btnSeeResult;
}
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return viewResult.bounds.size.height;
}
Anyway, what's the difference between setting the footer view directly like
tableView.tableFooterView = footerView;
and using (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section method??

There's no reason for the viewResult wrapper. A UIButton is a UIView, so there's no reason to wrap it in another UIView in this case - especially since you then assign the button to the table's footer property which means the table should be taking control of how and when the button view ultimately displays.
You may have to set the btnSeeResult's frame so that it has a height set. It's possible the height is 0 by default in that case since you don't ever set a frame for it. You might also try something like [btnSeeResult sizeToFit]; after you set the title which should resize it enough to make room for the label text, at least.
As to your second question, the difference between tableFooterView and -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section is that the former defines a view to attach to the bottom of the entire table, whereas the delegate method allows you to give each sub-section in your table different footers. Both can be used at the same time and in that case the last section's footer view would appear followed finally by the tableFooterView.

SeniorLee,
Welcome to StackOverflow!
You've got the right answer in your question - it's the latter code:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
When the table view goes to construct your table, it calls back to 2 classes - the table's delegate, and the table's data source.
Data Source is just what the name implies - the source of the data that is in your table. The Delegate, on the other hand, handles real-time interaction with the table and how it is to be displayed (e.g. what happens when the row is pressed?).
In many many cases, programmers set both the Data Source and the Delegate of the table view to the same class - the UITableViewController that owns the table view.
Anyhow, on to your question:
It's highly likely that the direct assignment isn't working explicitly because there is a delegate callback. When the table view goes to lay itself out, it will ask its delegate "do you have a view for the footer in section x?". That's the method above.
If you don't implement that method, the table view will have a default implementation that does something - in this case, probably set it to nil.
So, my guess is that even though you set this property directly in viewDidLoad, it's being overwritten to nil by the table view because you're not implementing the delegate callback. Instead, move your view code to the callback method and it should work beautifully.
Also, I don't think you need to wrap your UIButton in a UIView - UIButton is a subclass of UIView and thus should work just fine.

Related

UITableViewCells change size in edit mode

I want to change the appearance of an UITableviewCell in edit mode like it is shown in the address book from apple. The cell should resize and i will add UITextFields as subviews.
I know that to change appearance of a cell you have to overwrite the LayoutSubviews function in the cell. I tried to do that and i had some funny effects and resizing :-)
I have looked for a while to find some hints on the net but i didnt find one.
If anyone could provide some hints how to do this right? Links to tutorials or code will be fine.
Thanks
Eddy
It's NOT a good idea to overwrite setEditing:animated: and reload your table view cells there.
That is very resource-expensive, and not the right place to do it.
In the subclass of UITableViewCell, override method didtransitionToState:
There, you can act directly on the cell outlets, like so :
- (void)didTransitionToState:(UITableViewCellStateMask)state
{
[super didTransitionToState:state];
if (state == UITableViewCellStateShowingEditControlMask) {
// edit mode : peform operations on the cell outlets here
} else if (state ==UITableViewCellStateDefaultMask) {
// normal mode : back to normal
}
when you set myTable.editing=YES; it calls table view datasource and delegate method.
so if you have any data to display in table then the above code line calls the delgate method
so you can code here
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(myTable.editing==YES)
{
return 70;//customize it.
}
return 50;
}
If you want to resize the cells height you should also change "heightForRowAtIndexPath" in your UITableView Delegate.
At least that is a thing I stumbled over a few times.
Overwrite setEditing:animated: and reload your table view cells there.

Decrease UITableViewCell width and put custom button on the left

Is there any way to decrease the standard width of grouped UITableViewCell and put a custom button on the left side(outside of cell boundary)? I tried to change the cell size but it keeps same
You are going to have to fake the editing mode.
What I mean by that is that as AtomRiot said you have to subclass UITableViewCell so that when in editing mode you show the button you want on the left, outside the cell.
But first things first.
To change the indentation level for your cells all you need to do is implement this delegate method for the UITableView
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
So that takes care of it. Then in your UITableViewCell subclass all I would do is to implement the method
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
which I assume is called when the table the cell belongs to has changed to editing mode.
There I would fade in (or animate in any way you want) a button to appear on the left of your cell.
I have done it inside a grouped-style cell but never on the outside.
Give it a try!
You could subclass UITableCell and add your own custom views inside of it. I have not personally added a button inside one but it should work. It may get confused with the row selected call the tableview makes if you are implementing that.
The Cocoanetics blog seems to have a pretty good solution to this:
http://www.cocoanetics.com/2010/03/how-to-shrink-cells/

Look like iPhone inbuilt contact applications image selection

I have created an application in which i have to add users to the sqlite database.
Now the problem is I want the look of the standard iPhone Contact application Where while adding user we have the width of first cell smaller than other cells and the image before that cell..
Can you please give me the idea how such thing is possible.
How to make one cell small and rest others of normal size..
Thanks for any help in advance
There are three UITableViewDelegate messages you can listen for to adjust height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
However, even thought I didn't write Contacts.app I have a feeling they are also using
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
To adjust the views as well. Remember, you don't have to pack everything into a single monolithic custom table view cell. You can create multiple custom table view cells and load them each appropriately depending on the index path.
The contact detail view is a grouped tableview. Each cluster of cells is a section. The top section is a single custom cell with two subviews that look like squashed tableview cells. The left view shows the contact's photo. The right view shows the name.
To reproduce, create a custom UITableView subclass and lay it out like you want either programmatically or in Interface Builder. Then in the tableview delegate's cellForRowAtIndexPath check indexPath.section and return the proper row for the section.
It appears that the Contacts app uses a custom tableHeaderView when presenting the contact details with an image and label. A similar implementation is included in the sample project iPhoneCoreDataRecipes. The RecipeDetailView loads a separate nib in tableViewHeaderView that is used to set the tableView.tableHeaderView property. Have a look at RecipeDetailViewController.{h,m} and DetailHeaderView.xib. When the Contacts app switches to editing mode, the headerView appears to be swapped out for another view that has a button and a tableView with a single cell. This will allow you to set up a separate tableViewDelegate to handle the Name parts of the contact and a delegate to handle the address / telephony details.

UITableView has a custom section header view which disappears

The tableview, custom table header, and custom section headers are loaded from a NIB. The tableview is grouped.
When the view loads, the first section header doesn't show up. If I scroll down, the other section headers will appear at first, but will disappear as soon as the section above them touches the top of the screen.
If I scroll back up so that a disappeared section header is off the screen, then scroll back down, it will usually reappear.
The problem is fairly consistent but not entirely- sometimes I have to scroll up and down several times to get a header to reappear. Any ideas as to what could cause this?
I fixed it- I was using one UIView for 3 sections, changing the text appropriately then returning it. Creating separate UIViews in the NIB for each section fixed the problem. So I guess you can't do that.
Your custom views must be (or descend from) UILable or UIImageView objects. You may need to manually set row height for the headers to get custom views to load and display properly.
You should review the details in the UITableViewDelegate protocol.
Specifically, look at these methods:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Part of the discussion on the tableView:viewForHeaderInSection: method states:
This method only works correctly when tableView:heightForHeaderInSection: is also implemented.

Relative Height of UITableview

In RootViewController am using a UITableView for displaying the content of data. In the didSelectRowAtIndexPath method, I am calling another view controller (DetailViewController) to display detailed data. For displaying this detailed data I am using a UITableview in the DetailViewController also. This table contains one section and only one row.
Now the problem is that I have to adjust the table's height dynamically when I move from RootViewController to DetailViewController. How can I make the height of the UITableView dynamic between the two classes?
Any help would be Appreciated!
You can implement the UITableViewDelegate method something like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [TextToDisplay sizeWithFont:/*DESIRED_FONT*/ constrainedToSize:/*YOUR DESIRED_SIZE*/ lineBreakMode:/*DESIRED_LINEBREAKMODE*/].height;
}
to get variable heights.
It sounds like your trying to reuse a UITableView between two different UIViewControllers. I think your better off having the RootViewController have it's own UITableView which is set to a dynamic height and width using the UIView autoresizingMask property. Then when you select a row and push on the DetailViewController in a UINavigationController stack it would have it'd own UITableView to display the detail information your trying to show.
This interaction technique is used throughout other iPhone applications like Mail. If I misunderstood your question please let me know.