image view with table view in the same view controller - iphone

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.

Related

use table view or A view and three Subview or use labels

i am a new Programmer....so i wants to know... what i use (table view,
or view and three subviews, or i use labels for it)
for generate this view on button click.... i do everything programatically...
CGRect cgRct = CGRectMake(0.0, 0.0, 480, 320); //define size and position of view
myView = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(4,80,312,325) style:UITableViewStylePlain];
[table setDataSource:self];
[table setDelegate:self];
i am confused...:(
thanks in advance
It's a TableView with 3 different kind of subclassed UITableViewCells. This is alot of work so I can't just give you the code to do this. Instead I'll give a summary on which steps to take.
In short, to replicate this you'll need to:
Make 3 different UITableViewCell subclasses.
For exmaple the first contains 7 UILabels and an UIImageView.
Make a UITableView class which loads these 3 cells. Basicly like this,
(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) return cell1;
else if (indexPath.row == 1) return cell2;
else return cell3;
}
Set variable rowHeights via this method:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Set the tableview properties to have a UIImage as background + set border color. Also note that the tableviewstyle is grouped.
Alternatively you could create some images in photoshop and add the labels. Much easier but not very dynamic at all.
Depends on what you wanna get. If the view is fixed size is a lot easyer have it as subview, with proper background image, labels, buttons...
But if you can have more than the shown 3 "rows" table view with different custom cells is probably more flexible.

(iphone) UIViewController having UITableViewController as member to make tableView as it's subview?

I want a viewController's view have a tableView as its subview.
Since I need to override some methods of tableViewController such as
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
, I had my viewController to possess (custom)TableViewController instead of tableView.
Then, I do something like,
UIView* holderView = [[[UIView alloc] initWithFrame:] autorelease];
[self.view addSubview holderView]; //self is viewController
[holderView addSubview: tableViewController.view];
.. change tableViewController.view.backgroundColor so on..
then, tableView magically(?) fills the holderView and works fine.
But I sense this is rather irregular and wonder what's the best practice to have a customized table view as subview?
Thank you
What I did (and is done more commonly I believe), is to make the viewController the table's delegate as well. That means that controller has functions like viewDidLoad, but it also needs the numberOfRowsInSection, cellForRowAtIndexPath, etcetera..
When setting the delegate of the table to the viewController (self), it means the table will look for the information required to build the table on that delegate. The delegate contains those table-functions.
EDIT: I just saw the 'custom' prefix on your table. In that case, I'd still use my described method but use CustomCells instead.

can I resize UITableView?

I want something like an image and table as the image will finish and data on table, so is it possible, as designed below
+++++
IMAGE
+++++
---------
---------
---------
------- > showing table cells, so first there should be image then a resized UITableView?
I dont understand your question... but as far as I understand you can set an imageView in the header of the tableView to hold your image
EDIT
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGRect frame;
frame = CGRectMake(0, 0, 320, 40);
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
imageView.image = someImage;
imageView.opaque = YES;
return imageView;
}
Just add something like this in your View Controller.
your can also use,
[tableView setTableHeaderView:yourImageView];
This will show the Image on the header of the table view.
Use this code, where you are allocating / creating tableview
You should subclass a UIViewController, and implement the UITableViewDelegate and UITableViewDataSource protocols. And then in the viewDidLoad method create a UITableView and add it to self.view. You'll also need to create a retain property called tableView and set it to the Tableview you created in viewDidLoad.
When you create the tableView you can set it's frame to anything you want, but remember to set it's autoresizing mask appositely (normally o flexible width and flexible height)
Then you can add your imageView to above the tableview

How can I add an image into a tableview "header"?

I have a UITableView where I am displaying a list of data. I would like to have a logo instead of just text for the header portion of the view. The code below gives me just the text.
Any ideas on how to get the image in there?
- (void)viewDidLoad
{
[super viewDidLoad];
self. = #"Videos";
Something like that:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 69.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 69.0)];
headerView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: #"header1" ofType: #"png"]]];
return headerView;
}
To get an icon in the navigation bar (basically what the Facebook app does) use the navigationItem's titleView property. You can put an arbitrary UIView there, and a UIImageView with the logo on a transparent background would give the same effect that Facebook uses. Do this in viewDidLoad and you're set.
Any view controller can use a UINavigationItem, so if you're not using a navigation controller you should still be able to get one.
Alternately, just add a UIToolbar wherever it makes sense, and give it a UIBarButtonItem with a custom view set to a UIImageView.

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.