can I resize UITableView? - iphone

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

Related

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.

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.

How can I stop my custom UITableViewCell backgroundImage from being stretchable?

If I create a background image for my table cell in the init method of my UITableViewCell subclass, the image comes out exactly how I drew it, and tableView:heightForRowAtIndexPath: simply adds a gap around it.
But if instead I create the background image in my tableView's tableView:cellForRowAtIndexPath: method and set it, changing the cell height will now stretch the image.
I need to create the background views in the cellForRowAtIndexPath method, as the background changes for different rows. How can I make my image's size fixed again?
Have you tried setting the contentMode to UIViewContentModeCenter for the backgroundView of the cell?
put it here
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundView.contentMode = UIViewContentModeCenter;
}
You could create something with:
UIView *cellBackView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
<insert image>;
cell.backgroundView = cellBackView;
My guess is the backgroundView gets strechted automatically, but you could make sure the image isn't stretched on that view by setting a width and height. This way, you can do whatever you like with that view.

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.

UITableViewCell / UISegmentedControl border issue

I'm trying to get a UISegmentedControl in a group UITableViewCell much like in the wifi settings in the Setting Application. The problem I'm having is I'm getting a double border. I get one border for the UISegmentedControl and one for the UITableViewCell.
I'm guessing I need to remove the border from the UITableViewCell. How can I go about doing that?
I just noticed this is still getting answers. As it happens I've had to do this for another project and since I asked this question I've learned a lot more about iPhone dev. Here is how I solved it recently. It's all about making the frame the correct size. This should do it for a standard table.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if(cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CellIdentifier"] autorelease];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(-1.0f, -1.0f, 302.0f, 46.0f)];
[cell.contentView addSubview:segmentedControl];
In the case of the Wi-Fi settings, I suspect what they've done is made the "Forget this Network" button, the "IP Address" label, and the "DHCP/BootP/Static" segmented control all part of the table's header view. If you need to do this in the middle of your table (as opposed to at the top or bottom, for which you'd use the tableHeaderView and tableFooterView properties respectively), I'd suggest using the delegate methods -tableView:viewForHeaderInSection: with -tableView:heightForHeaderInSection, or the corresponding Footer variants. With any of those, you'd set up a custom view for that "section" of your table view (using either a clear background color or [UIColor groupTableBackgroundColor]), containing a label and a segmented control arranged so that they match up with the rest of the table sections.
Using the technique in this post to remove the background opacity of the UITableViewCell worked more easily for me to get only the UISegmentedControl to show in the table row.
I've got slightly further with this. So far I've subclassed UITableViewCell. I created a nib with a UISegmentedControl in it and I set the UITableViewCell background alpha to 0. It still doesn't look quite right, but it's better than before.
My solution is to allow the segmented control to resize to fit, and to hide the table view's background in tableView:willDisplayCell:forRowAtIndexPath:.
This yields results identical to the "Settings.app > WiFi > Your Network > IP Address" Segmented Control without hard-coding any layout metrics:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"One", #"Two", #"Three", nil]];
control.segmentedControlStyle = UISegmentedControlStylePlain;
control.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
control.frame = cell.contentView.bounds;
[cell.contentView addSubview:control];
[control release];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundView.alpha = 0.0;
}
The trick appears to be to size the UISegmentedControl to the size of the backgroundView of the control, not the contentView. I was able to do it programmatically by doing the following:
// Size to cover the entire background
self.contentView.frame = self.backgroundView.frame;
self.myControl.frame = self.contentView.bounds;
Note that if you are using an accessory, you need to account for the accessoryView as well.
The reason is that the view hierarchy is as follows:
self (the UITableViewCell or subclass)
backgroundView
contentView
(your controls go here)
accessoryView
In portrait layout, the backgroundView's frame is {{9, 0}, {302, 44}}, whereas the contentView's frame is slightly smaller, at {{10, 1}, {300, 42}}. This gives the cell its 1px "border" when the table style is grouped. You have to resize both the contentView and your control to get the appropriate size.
(NOTE: While Apple actually has several examples of a UISegmentedControl in the UICatalog sample code project in the SDK, they effectively "cheat" by using a UIViewController and setting the main view's background color to the table background color.)