Im trying to add views inside custom UITableViewCells. I tried using the [cell.contentView addSubview] method but the views dont appear. What is it I have to do. Thanks
if you are using custom cell then go to your custom cell class and ...
UIView *contentView;
contentView =[[UIView alloc]init];
then set frames of your view...
finally
[self.contentView addSubview:contentView];
Related
hi i'm studying iOS programming.
i have a wondering using interface builder.
i make a tableViewController and also make .xib file
now i can see the UITableView in interface builder.
i have to add a view, called myView, that contains buttons, labels and so on.
i want to myView be set the top of tableView's area, like tableview's header.
so i make a myView, and add buttons, labels, etc.
and i drag that view into UITableView. ok here's fine.
i can see myView is set the top of UITableView in interface builder.
but i run the program, myView doesn't appear.
of course wire up using IBOutlet, and declare property and synthesize.
but i use NSLog like this
if(self.myView == nil)
NSLog(#"omg it's nil!!!");
i can't understand that NSLog is printed my prompt area.
because i make that view in interface builder!
i can see tableView, of course can see the cells.
why myView doesn't appear??
how can i fix it??
i want to know how can i fix it using interface builder.
please help me
Not sure if this is possible using interface builder, I usually create a view manually and add it to the tableview header in the viewWillAppear method like so:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 60)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10,self.tableView.bounds.size.width, 50)];
label.text = [person getFullName];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:25];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0,1);
[headerView addSubview:label];
self.tableView.tableHeaderView = headerView;
A simpler method would be to create a view in a separate nib file and then load it into the table header when you load the tableview like this:
UIViewController *aViewController = [[UIViewController alloc] initWithNibName:#"MYXIBFILEHERE" bundle:nil];
self.tableView.tableHeaderView = aViewController.view;
My experience with XCode 4.3:
you have to first place UITableView and UIView on the same super UIView. Then, when dragging UIView to the top of UITableView you'll notice XCode highlighting the option of inserting your view as footer.
Once done, you can detach UITableView from superview and delete it.
The only way I've been able to get this to work is as follows:
Add new nib file to project. Lay out the view in the nib file as you
want your table footer/header to look.
Set the class of the File's Owner in the nib to the class of your
UITableViewController. This is so we can connect the view in this
nib to an IBOutlet in the UITableViewController class.
Set a UIView IBOutlet in your UITableViewController to point to the
view in the new nib.
In your UITableViewController class, do something like this in
viewDidLoad:
(apparently the code won't format correctly if it's directly after a numbered list.)
[[NSBundle mainBundle] loadNibNamed:#"statsFooterView" owner:self options:nil];
self.tableView.tableFooterView = myFooterView;
I've got an app with several UITableViews in it, and if one of them is empty, I'd like to display a bit of help text, floating in front of the UITableView:
"Looks like you haven't created anything for this list yet. Make your first entry by tapping the button above."
How do I present this view, floating in front of the UITableView?
What method of the UITableViewDelegate would be used to present this view?
What would the code look like to present the view?
What method of the UITableViewDelegate would be used to hide this view once the user adds content?
Here's a quick mockup:
declare in .h file
UIView *root;
in .m file
import QuartzCore/QuartzCore.h
- (void)viewDidLoad
{
[super viewDidLoad];
root = [[UIView alloc] initWithFrame:CGRectMake(60, 50, 220, 250)];
root.backgroundColor=[UIColor grayColor];
root.layer.cornerRadius=10;
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 50)];
label.backgroundColor=[UIColor clearColor];
label.numberOfLines=3;
label.font=[UIFont fontWithName:#"Arial" size:14];
label.lineBreakMode=UILineBreakModeTailTruncation;
label.textColor=[UIColor whiteColor];
label.text=#"Make your first entry by tapping the button at the top of the screen";
[root addSubview:label];
[self.view addSubview:root];
}
inside your button Event method
[root removeFromSuperview];
in viewDidLoad/Appear,
create a view and add it as subview [self.view addSubview:infoView] . on click event of + button remove it from superview [infoView removeFromSuperView]
I would create/remove the view in numberOfRowsInSection (if it is 0 then show the view) or whatever it is called. As for the view itself, have a UILabel in a custom view. To add the rounded corners etc you can access the view's layer, which is a CALayer. Oh and make sure that you have an ivar for the view so you can remove it easily and check if it is visible. And make sure you always reload the table view when you show your view, as otherwise numberOfRowsInSection will not be called.
I have to create the custom TableView with the picker control in multiple rows,can anyone help me on this?
You can add the UIPickerView as a subView of UITableViewCell.
[cell.contentView addSubView:pickerView];
Don't forget to return the appropriate height from heightForRowAtIndexPath: method.
if (cellContainsPickerView) return 200.0; // The height of the picker view
In my UITableView, I have a tableHeaderView that should resize itself according to the content. The resizing works fine, however, the tableHeaderView obscures the first couple of cells. Apparantly changing the frame of the tableHeaderView doesn't reorganize the rest of the view.
I've already tried calling [self.tableView layoutSubViews] and [self.tableView setNeedsLayout], but those don't help either. Also tried giving the tableHeaderView from the Interface Builder an outlet to change the frame on that, but that doesn't reorganize the tableView either.
How can I change the size of the tableHeaderView, without hiding any cells, reorganizing the rest of the tableView?
Try setting the tableHeaderView property of the UITableView after you resized your content.
[self.tableView setTableHeaderView:self.myResizedTableHeaderView];
Solved the problem for me. Hope it helps.
I don't know how you created you UITableView, but I tried to create one with a tableHeaderView like you and it's resizing.
I created a class inheriting from UITableViewController
#interface MyTableViewController : UITableViewController {
}
#end
and for the implementation, in the viewDidLoad, I added:
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[header setBackgroundColor:[UIColor redColor]];
self.tableView.tableHeaderView = header;
I have grouped table view. I want to add UISegmentedControl in table cell with clear background so that it will be displayed as FOOTER . How can I do this? I tried using clearColor. But it's not working.
Set the background view with a clear UIView object like so:
UIView *clearView = [[UIView alloc] initWithFrame:CGRectZero];
[clearView setBackgroundColor:[UIColor clearColor]];
[self setBackgroundView:clearView];
[clearView release];
And make sure you set cell selection style to none.
Edit: added some square brackets