I have a UITableView that is blank be default until the user edits and adds data to it. I want to show an image with instructions that is shown until the user edits it.
The size of the picture is so it fits under perfectly between the nav bar and the tab bar.
Is there a way to do this programmatically?
you can use removeFromSuperview to remove UITableView from super view then add your UIImageView.
Use the below code :
-(void) NeedView:(BOOL) aIsImageView
{
if(aIsImageView)
{
[m_TableView removeFromSuperview];
[self.view addSubviews:m_ImageView];
}
else
{
[m_ImageView removeFromSuperview];
[self.view addSubviews:m_TableView];
}
}
Create a UIView add your UITableView as a subview. Create a UIImageView with your instructions image and add it as a subView as well. Position the instructions imageView behind the the tableView. In ViewWillAppear add a test for empty table and hide the table based on that check. If you allow users to delete rows you will also have to check for empty table in:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
// do delete
// get count of rows in UITableView and hide table if it's empty
}
...
It's not that difficult to achieve, you just can show the image you want to show with the specified frame (may be frame(0, 44, 320, 392)). and check for the table rows(actually the count of array that u are using to show in table) if it is nonzero just show the image and show the table otherwise.
try this concept and come back with any problem or query..
Related
This question already has answers here:
tableFooterView property doesn't fix the footer at the bottom of the table view
(10 answers)
Closed 8 years ago.
I have a UITableView with a footer, filled with a tabBar in a custom view, done using the following code:
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section {
//differ between your sections or if you
//have only on section return a static value
return 49;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
//allocate the view if it doesn't exist yet
footerView = [[UIView alloc] init];
[footerView addSubview:self.tabBarView];
}
//return the view for the footer
return footerView;
}
Which is working lovely, apart from when the table has less rows than are needed to fill the screen, this causes the footer to move up the screen, as the table no longer creates empty rows, due to having a footer.
So, does anyone know of a way to either lock the custom footer to the bottom of the screen, or, to make the tableView create empty rows as it used to do?
Thanks!
Gareth
Unfortunately I don't think there is an easy way to do this, other than some view hierarchy trickery. When the contentSize of your UITableView is less than the frame size, you assign the footer view to self.view and position manually. When the contentSize of your UITableView is greater than the frame size, you use viewForFooterInSection. Let me know if this isn't clear or if you'd like to see some sample code on how to do this.
I have the same problem for uitableview controller, I solved this by adding view in the window and remove this object in viewWillDisappear.
My Solution link
I am trying to set a custom background for the rows of a grouped UITableView and I have absolutely no idea how to achieve that.
The table design I am trying to apply on my table looks like this:
And I have sliced it into 3 cell types: top, middle and bottom. How can I apply the custom background for each of the table's cells?
You can set UIImageView as cell backgroundView for first, middle and last cell in function:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Check good (and simple) example at:
http://www.planet1107.net/blog/tips-tutorials/custom-grouped-cell-tutorial/
In cellForRowAtIndexPath, check the type of current row and then assign the corresponding background to it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create cell
// ...
// Configure cell
switch (cellType) {
case TOP:
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"top.png"]];
break;
case MIDDLE:
...
}
}
Ray Wenderlich has a very good tutorial on how to do this with Core Graphics: http://www.raywenderlich.com/2033/core-graphics-101-lines-rectangles-and-gradients
This tutorial will give you exactly what you're looking for.
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
UITableViewCell has an property backgroundView. Set this value in your tableView:cellForRowAtIndexPath:-method.
https://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
Instead of assigning separate image for each, Create one custom cell and assign background image as cell has above. And Crop above image other than inner cells assign that one to UITableView's background view (Plain table view) i.e you dont need to create grouped table view.
I can subclass the cell and resolve the problem, but I'd like to know why this doesn't work.
I set an image in the cell, then in this method, I move the image to the right:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell.imageView setFrame:CGRectMake(cell.contentView.bounds.size.width/2 - cell.imageView.frame.size.width/2 + 100,
cell.contentView.bounds.size.height/2 - cell.imageView.frame.size.height/2,
cell.imageView.frame.size.width,
cell.imageView.frame.size.height)];
NSLog(#"center: %#", NSStringFromCGPoint(cell.imageView.center));
NSLog(#"frame: %#", NSStringFromCGRect(cell.imageView.frame));
}
Notice the +100 I added to ensure it's over to the right.
When I check the console, it indicates this:
center: {250, 57.5}
frame: {{150, 7.5}, {200, 100}}
However the output is this:
The image is still aligned to the left side. Can this be changed, and if so, how?
if you go to the header file of UITableViewCell than there you will find...
> // Content. These properties provide direct access to the internal
> label and image views used by the table view cell. These should be
> used instead of the content properties below.
> #property(nonatomic,readonly,retain) UIImageView *imageView
> __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // default is nil. image view will be created if necessary.
so as you can see it says readOnly property to the image view... I think this explains your inability to move the imageView.
You can not move the image view. Solution is or to subclass and do your own drawing in drawRect, or to add a subview to the cell.contentView.
Edit:
If an image is set, it appears on the left side of the cell, before
any label. UITableViewCell creates the image-view object when you
create the cell.
From UITableViewCell Class Reference
Im using a tableview to display some information in a quiz app that Im working on. My question is how do i make the tableview only show the number of cells that I need. Ive set the number of rows delegate method like this:
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
but at the bottom of the table view are empty cells that are not needed. If I set the tableview style to grouped I get 5 cells and no empty ones below them. Ive seen that other people have done this but cant seem to work it out. I was wondering if they have somehow added a custom view to the table footer to cancel the empty cells out?
Any ideas or help appreciated.
If you do want to keep the separator, you could insert a dummy footer view. This will limit the tableview to only show the amount of cells you returned in tableView:numberOfRowsInSection:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
In swift:
self.tableView.tableFooterView = UIView(frame: CGRectZero)
A much nicer method which doesn't require cell resizing is to turn off the default separator (set the style to none) and then have a separator line in the cell itself.
I was having a similar problem, how to show only separators for the cells that contain data.
What I did was the following:
Disable separators for the whole tableView. You can do that in the
inspector for the tableview in Interface builder or by calling
[yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];.
Inside your cellForRowAtIndexPath where you populate your tableview with cells create a new UIView and set it as a subview to the cell. Have the background of this view lightgray and slightly transparent. You can do that with the following:
UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake:
(0, cell.frame.size.height-1,
cell.frame.size.width, 1)];
[separatorView setBackgroundColor:[UIColor lightGrayColor]];
[separatorView setAlpha:0.8f];
[cell addSubView:separatorView];
The width of this view is 1 pixel which is the same as the default separator, it runs the length of the cell, at the bottom.
Since cellForRowAtIndexPath is only called as often as you have specified in numberOfRowsInSection these subviews will only be created for the cells that possess data and should have a separator.
Hope this helps.
This worked for me - I had extra empty rows at the bottom of the screen on an iphone 5 -
In my case I needed 9 rows
- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.tableView.frame.size.height / 9;
}
You can implement heightForRowAtIndexPath: and compute the correct height to only show 5 cells on the screen.
Are you always going to have 5 rows? If it's a dynamic situation you should set the number of rows according to the datasource of the tableview. For example:
return [postListData count];
This returns the count of the records in the array holding the content.
The tableview is only going to display the number of rows and sections that you tell it to. If you're always going to have just a single section, DON'T implement the method below.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
Without this the tableview will only have 1 section. With it, as you would imagine, you can specify the number of sections.
It is quite Simple. Just set the size of the popover like this:
self.optionPickerPopOver.popoverContentSize = CGSizeMake(200, 200);
Certainly you can adjust the size (200,200) depending upon the size of contents and number if rows.
Easy way would be to shrink tableView size. I.e. 5 cells 20 points each gives 100.0f, setting height to 100.0f will cause only 5 rows will be visible. Another way would be to return more rows, but rows 6,7 and so would be some views with alpha 0, but that seems cumbersome. Have you tried to return some clerColor view as footerView?
I think u can try changing the frame of the table view, if you want to adjust with the number of cells.
Try something like this:
[table setFrame:CGRectMake(x, y, width, height*[list count])];
height refers to height of the cell
As Nyx0uf said, limiting the size of the cell can accomplish this. For example:
- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat result;
result = 100;
return result;
}
implement these two methods in your UITableViewController:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return 1;
}
return 0;
}
In fact, these codes are telling tableview that you don't need to render the seperator line for me anymore, so that it looks the empty cells won't be displayed(in fact , the empty cell can not be selected too)
I've got a nice table populated with data, with custom-designed cell layouts. Currently, tapping a cell will push in a new view (for more information, say), which works fine.
What I'd like to do, though, is have the new view push into the cell's bounds, not fill up the whole screen. Can't seem to wrap my brain around how to accomplish this -- any pointers you could offer?
Thanks! :)
Update: Here's some of the code, in case that helps the helpers:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
FlippedViewController *flippedViewController = [[FlippedViewController alloc] initWithNibName:#"FlippedView" bundle:nil];
// What goes in here? Where do contentView and originalView come from?
[originalView removeFromSuperview];
[contentView addSubview:flippedViewController.view];
[flippedViewController release];
}
You can add a hook in tableView:didDeselectRowAtIndexPath: (a method of UITableViewDelegate) which uses a pointer to the particular cell's contentView, and performs the changes on that UIView just as it would any other.
For example, if you wanted to simply replace originalView (assuming that's what's in the cell now) with newView, you could do something like this:
// Use your own data structure and indexPath to retrieve contentView.
[originalView removeFromSuperview];
[contentView addSubview:newView];
That would update the view without an animation.
About the animation -- here are two related previous questions:
UIView based animation
Animating the display of a view