Fixed button at bottom of UITableView - iphone

I have a view controller which has a tableview subview. I have made the tableview height smaller, leaving about 46 pixels for a button that needs to stay at the bottom and not scrolled with the table.
I have played around changing the height of the table view (in IB) and it doesn't seem to change anything. Any help would be appreciated.

You need to have the UIButton at the same level as the UITableView - both are subviews of the main view. Do you have the UITableView as the view of the view controller?

This can be done programmatically in the UITableViewDelegate:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// Build a custom view
// ...
// Build a custom button
// ...
[customFooterView addSubview:button];
return [customFooterView autorelease];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 53.0; // return your button's height
}

Related

Custom IOS separator

How is the 'or' separator below made? is it an image used in the section header?
if that's really done with a table view, then it's done via a delegate method of "tableView:viewForHeaderInSection:".
Yes, it is an image used for section header.
That login page is modifying the header/footer views of the UITableView.
When you are setting up the table view you need use the following UITableView delegate methods in order to create a header and footer view in each section of your table view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// Set your custom view for the header in the section
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// Set your custom view for the footer in the section
}
- (CGFloat) tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
// Set the height of the header in each section
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
// Set the height of the footer in each section
}
NOTE: If you notice the first two methods return a UIView therefore it does not just have to be an image. You can create any custom view you want and place it in the tableView header/footer. The view could be UIKit elements, UIImage, or even made with CoreGraphics.

How to hide empty UITableViewCells showing in UITableView with plain style - IOS?

UItableView is showing with empty rows like below...
how to hide it without changing the tableview style. Do I need to resize the table View, if so how can I know the height exactly to set.
self.tableView.tableFooterView = [[UIView alloc] init];
you add UIview in footer of uitabeview use of xib or tableview delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
return yourview;
}
What I had done in one of my projects was to set the tableview's background color to clearColor and separator to none.
Just drag and drop a view from object library to the TableView empty shell in storyboard. Make the view as footer of the table view (in document outline drag it below the cell's if it is not already at below) make the height of the view '0'. You will not see the view in storyboard because its height is '0' but when its runs it will hide the cells. I think that should work.
Why don't you put some logic into your
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
function?
Just return the number of cells which are really filled with data

How to custimize the height of Table Cell with respect to height of image in it?

i have a UITableView and i am showing different images in cells,each image has different height,how can i customize the cell height with respect to heights of images ?
Implement the UITableViewDelegate method – tableView:heightForRowAtIndexPath:. See the documentation.
For example, if you're using Interface Builder, right-button-drag from your table view to the controller itself, add <UITableViewDelegate> after UIViewController in your view controller's class declaration, and add something like this to the the class .m file:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = imagesArray[indexPath.row];
return image.size.height + 5; // 5 = margin.
}

image view with table view in the same view controller

I have a new controller defined as follows:
#interface view1: UITableViewController
I've created (in viewDidLoad) an image view (logo image) and added this image view as subview for view1, but the problem is that table view cells still appear behind this image view, how can completely separate the image view from the table view ?
thanks in advance.
To have a logo type view you either need to set a custom headerview for the tableview via
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
and
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
the other method would be overriding -loadView and creating your own view that has two subviews, your imageview and a tableview.
In the first method, once your scroll some the logo will eventually disappear. The second method makes the logo static.
Try adding it in:
- (void)viewDidAppear:(BOOL)animated
This method is only called once you have called viewDidLoad so if you want something over everything else you might call this one or add the subview to:
[[[UIApplication sharedApplication] keyWindow] addSubview:yourView];
Hopefully it helps you.
2 options:
1.create a UIViewController to hold your UITableViewController controller view and your imageView, then position their frame so they wont overlap
2.add the imageView as a TableView Section Header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView* imageView = [[UIImageView alloc] initWithImage:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"]]];
return imageView;
}
and make sure you have at least 1 section of course in:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
What is seems like you want is for your logo to be at the top, above your table view? If so then you can, in -viewDidLoad, set the tableView's tableHeaderView to the view you want, e.g.:
tableView.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image"]]; // assuming ARC, else autorelease or put in variable first...
If you want it to float on top when scrolling then DanZimm's use of -tableView:viewForHeaderInSection: is what you want.

How to get the size of the "Delete" button in a UITableViewCell when swipe to delete?

I add a datetime label on the right of a table cell. When swip-to-delete shows the "Delete" button, the datetime label need to shift left a little bit. But how to get the "Delete" button's size?
I tried to find it in the cell.subviews but failed.
You don't have to know the button's size. Instead, use the size of the cell's contentView property to calculate the sizes of the subviews. When swiping over a cell, UIKit will adapt the contentView's size and call layoutSubviews on the cell-object. In your subclass of UITableViewCell, overwrite the layoutSubviews method and set the appropriate sizes to the subviews.
Look at RecipeTableViewCell.m of Apple's iPhoneCoreDataRecipes sample code.
Use this code in your custom Cell class
- (void)layoutSubviews {
[super layoutSubviews];
NSMutableArray *subviews = [self.subviews mutableCopy];
while (subviews.count > 0)
{
UIView *subV = subviews[0];
[subviews removeObjectAtIndex:0];
if ([NSStringFromClass([subV class])isEqualToString:#"UITableViewCellDeleteConfirmationView"])
{
UIView *deleteButtonView = (UIView *)[self.subviews objectAtIndex:0];
CGFloat deleteBtnHeight=deleteButtonView.frame.size.height;//here you get the height
}
}
}
The size adjusts to fit the text contained. See the following code:
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"Dynamic width!";
}
vs
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"⏎";
}
If you don't override layoutSubviews method in your custom table view cell than my approach is:
Create your custom subview, set frame basing on contentView.bounds.
Set autoresizingMask to UIViewAutoresizingFlexibleWidth.
Add your custom subview to ContentView of a cell.
Configure cell for editing
Now when you swipe on cell the delete button appears and your view auto resizes with contentView.
The delete button is 63x33.